Maximum Unique Character Window
StringsAuto-graded · 26 test casesLast updated
Tests sliding window technique with hash map to track character positions and find the longest contiguous segment with all distinct characters.
You are given a string s. Your task is to determine the length of the longest contiguous segment within the string where no character appears more than once.
A contiguous segment is a sequence of consecutive characters from the string. For example, in "hello", the segments "hel" and "llo" are contiguous, but "hlo" is not.
Write a function that returns the length of the longest such segment.
Constraints
0 <= len(s) <= 50000- The string may contain English letters (both cases), digits, symbols, and spaces
- An empty string has a longest unique segment of length 0
Examples
Example 1
Input: length_of_longest_substring('abcabcbb')
Output: 3
Example 2
Input: length_of_longest_substring('bbbbb')
Output: 1
Example 3
Input: length_of_longest_substring('pwwkew')
Output: 3
Hints
Hint 1
Think about maintaining a window of valid characters and expanding it as you scan through the string
Hint 2
Use a dictionary to remember where each character was last seen so you can quickly adjust the window when you encounter a duplicate
Hint 3
When you find a repeated character, you need to move the left boundary of your window just past the previous occurrence of that character
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We use a sliding window technique combined with a hash map to efficiently track valid segments. The core insight is that when we encounter a duplicate character, we don't need to check every possible starting position—we can jump directly past the previous occurrence.
We maintain a window [start, i] representing our current candidate segment. As we scan through the string:
- Keep a dictionary mapping each character to its most recent index
- When we see a character that already exists in our current window, move the window's left edge to just after the previous occurrence
- Track the maximum window size we've seen
The critical detail is checking whether the previous occurrence is actually inside our current window. A character might still be in our dictionary from an earlier position that we've already moved past. We only need to adjust our window if the previous occurrence is at or after our current start position.
Why It Works
At each position i, we maintain the invariant that s[start:i+1] is the longest segment ending at position i with all distinct characters. When we add a new character:
- If it doesn't repeat within the window, we simply extend the window
- If it does repeat at position
jwithin the window, any segment that includes both positionsjandicontains a duplicate. The leftmost valid starting position is thereforej + 1
By checking the maximum window size at each position, we're effectively checking all possible segments in a single pass.
Complexity
Time complexity: O(n) where n is the length of the string. We visit each character exactly once, and all dictionary operations are O(1) average case.
Space complexity: O(min(n, k)) where k is the size of the character set. The dictionary stores at most one entry per distinct character. For ASCII this is effectively O(1).
Solution
def length_of_longest_substring(s: str) -> int:
last_seen = {}
start = 0
best = 0
for i, ch in enumerate(s):
if ch in last_seen and last_seen[ch] >= start:
start = last_seen[ch] + 1
last_seen[ch] = i
best = max(best, i - start + 1)
return best