Longest Distinct Run and First Singleton Position
StringsAuto-graded · 22 test casesLast updated
Tests a sliding window with last-seen indices plus a frequency count to find the longest repeat-free substring and the earliest character occurring exactly once.
You are given a string text made up of lowercase English letters. Compute two values about it and return them together.
- Longest distinct run: the length of the longest contiguous substring in which no letter appears more than once.
- First singleton position: the smallest index
isuch that the lettertext[i]occurs exactly once in the entire string. If no letter occurs exactly once, this value is-1.
Return a two-element list [longest_run, first_singleton].
Details
- A substring is a contiguous block of characters; a subsequence that skips characters does not count.
- For the empty string, the longest run has length
0and there is no singleton, so the result is[0, -1]. - "Occurs exactly once" is judged against the whole string, not just a window.
- The result must be a list of two integers in the order shown above.
Constraints
textcontains only lowercase English letters ('a'to'z').0 <= len(text) <= 10^5- Your solution should run in roughly linear time.
Examples
Example 1
Input: longest_distinct_and_first_singleton('abcabcbb')
Output: [3, -1]
Example 2
Input: longest_distinct_and_first_singleton('leetcode')
Output: [5, 0]
Hints
Hint 1
For the longest run, keep a window [left, right] with no repeated letters and move right one step at a time. Track the most recent index of each letter.
Hint 2
When the new letter was last seen at an index that is at least left, move left to just after that index. If the previous occurrence is before left, it is outside the window and must not move left backward (think of 'abba').
Hint 3
A singleton depends on the whole string, so count every letter first, then scan from the left and return the first index whose letter has count 1, or -1 if none.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two answers are independent, so each gets its own linear pass (or two).
Longest distinct run (sliding window). Maintain a window text[left..right] that contains no repeated letter, and store the latest index at which each letter appeared in last_seen. As right advances to a letter ch:
- If
chwas last seen at an index>= left, that occurrence is inside the window, so movelefttolast_seen[ch] + 1. - If its last occurrence is before
left, it is already outside the window andleftstays put.
Then set last_seen[ch] = right and update the best length with right - left + 1.
First singleton (count, then scan). Whether a letter occurs exactly once is a property of the whole string, so first count all letters in one pass. Then scan left to right and return the first index whose letter has count 1; return -1 if the scan finds none.
Why it is correct
After processing right, the window is the longest repeat-free substring ending at right: any valid window ending there must begin after the previous occurrence of text[right], and left is set to exactly that boundary (never moving backward thanks to the >= left check). Taking the maximum across all right values covers every possible ending position, so the global maximum is found.
For the singleton, the counts are exact, and scanning in index order yields the smallest qualifying index by definition.
For example, "leetcode" gives a longest run of 5 (such as "etcod") and the first singleton is 'l' at index 0, so the result is [5, 0].
Complexity
- Time: O(n), since each pass touches each character once with O(1) average dictionary operations.
- Space: O(k) where k is the number of distinct letters, at most 26, so effectively O(1).
Reference solution
from typing import List
def longest_distinct_and_first_singleton(text: str) -> List[int]:
last_seen = {}
left = 0
longest = 0
for right, ch in enumerate(text):
if ch in last_seen and last_seen[ch] >= left:
left = last_seen[ch] + 1
last_seen[ch] = right
longest = max(longest, right - left + 1)
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
first_singleton = next((i for i, ch in enumerate(text) if counts[ch] == 1), -1)
return [longest, first_singleton]