Maximum Unique Character Window Length
StringsAuto-graded · 22 test casesLast updated
Tests sliding window technique with hash map indexing to efficiently find the longest substring with all distinct characters.
Write a function that finds the length of the longest contiguous substring within a given string where every character appears exactly once.
You are given a string s that may include any printable characters: lowercase and uppercase letters, digits, punctuation, and whitespace.
Your task is to return an integer representing the maximum length of any substring in which no character is repeated.
Constraints
0 ≤ len(s) ≤ 100,000- The string may be empty
- All characters are treated as distinct entities (case-sensitive, spaces count as characters)
- You must solve this in better than O(n²) time complexity
Notes
- A substring is a contiguous sequence of characters, not a subsequence
- If the string is empty, return
0 - Only the length is required, not the substring itself
Examples
Example 1
Input: max_unique_substring_length('abcabcbb')
Output: 3
Example 2
Input: max_unique_substring_length('bbbbb')
Output: 1
Example 3
Input: max_unique_substring_length('pwwkew')
Output: 3
Hints
Hint 1
Use a sliding window approach where you maintain a valid window of unique characters and expand it character by character.
Hint 2
Track the most recent position of each character you've seen using a dictionary.
Hint 3
When you encounter a duplicate character, you need to move the window start forward—but only if the previous occurrence is actually inside your current window.
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 character positions. The core idea is to maintain a window that always contains only unique characters, expanding it rightward through the string while adjusting the left boundary when duplicates appear.
The algorithm maintains two pointers defining a window [window_start, window_end] and a dictionary char_index storing the most recent index where each character was seen.
As we scan from left to right:
- For each new character at position
window_end, check if it exists in our tracking dictionary - If the character was seen before AND its previous position falls within our current window (meaning
char_index[char] >= window_start), we have a duplicate - Resolve the duplicate by jumping
window_startto one position past the previous occurrence:window_start = char_index[char] + 1 - Update the character's position in our dictionary to the current index
- Track the maximum window size seen so far
The critical detail is the condition char_index[char] >= window_start. Without this check, we might jump window_start backward to a position that's already been excluded from our window, breaking the invariant that our window contains only unique characters.
Consider the string "abba": when we reach the final 'a' at index 3, the first 'a' at index 0 is already outside our window (which starts at index 2 after processing the duplicate 'b'). Jumping back to index 1 would incorrectly include both 'b' characters.
Why It Works
The algorithm maintains the invariant that after processing each position, the window contains no duplicate characters and represents the longest such window ending at that position. Since window_start only moves forward (never backward), each character is visited at most twice: once when expanding the window and possibly once when shrinking it. This keeps the time complexity linear.
Complexity Analysis
Time Complexity: O(n) where n is the length of the string. Each character is processed exactly once by the main loop, and all dictionary operations (lookup, insertion, update) run in O(1) average time.
Space Complexity: O(min(n, k)) where k is the size of the character set. In the worst case where all characters are distinct, we store n entries. For fixed alphabets like ASCII (128 characters) or extended ASCII (256 characters), this is effectively O(1).
Solution Code
def max_unique_substring_length(s: str) -> int:
char_index = {}
window_start = 0
max_length = 0
for window_end, char in enumerate(s):
if char in char_index and char_index[char] >= window_start:
window_start = char_index[char] + 1
char_index[char] = window_end
max_length = max(max_length, window_end - window_start + 1)
return max_length