Longest Substring Without Repeating Characters
Master this topic with zero to advance depth.
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Visual Representation
s = "abcabcbb"
L R
| |
a b c a b c b b
[---] Set: {a, b, c}, Len: 3
L R
| |
a b c a b c b b
[---] Set: {b, c, a}, Len: 3Examples
The answer is "abc", with the length of 3.
Level I: Brute Force
Intuition
Check every possible substring and verify if it contains unique characters. A string of length has substrings. Checking each takes , leading to . We can optimize the check using a Set to .
Thought Process
- Use two nested loops to define every potential substring
s[i...j]. - For each substring, use a third loop or a Set to check for duplicates.
- If no duplicates, update the
maxLength.
Detailed Dry Run
| i | j | Substring | Unique? | Max Len |
|---|---|---|---|---|
| 0 | 0 | "a" | Yes | 1 |
| 0 | 1 | "ab" | Yes | 2 |
| 0 | 2 | "abc" | Yes | 3 |
| 0 | 3 | "abca" | No | 3 |
| 1 | 3 | "bca" | Yes | 3 |
Level II: Binary Search on Answer (O(N log N))
Intuition
We can binary search for the maximum length k. For a fixed k, we check if there exists any substring of length k with no repeating characters using a fixed-size sliding window. If a valid substring of length k exists, then any length smaller than k is also possible, so we search in [k, right]. Otherwise, we search in [left, k-1].
Thought Process
- Search range for maximum length is
[1, n]. - In each step of binary search (length
mid):- Use a sliding window of size
midto check for uniqueness. - If unique substring found,
ans = mid,low = mid + 1. - Else,
high = mid - 1.
- Use a sliding window of size
Pattern: Search on Answer Space
Level III: Optimal (Sliding Window)
Intuition
Categorization
This is a Variable Size Window problem because the length of the unique substring changes as we encounter duplicates.
Thought Process
Instead of checking all substrings, we maintain a sliding window [left, right]. As we expand right, we add the character to a Set. If s[right] is already in the Set, we have a violation. We must shrink the window from the left until the duplicate is gone.
Mandatory Variables
left: Marks the start of the current unique substring.charSet: Stores characters in the current window for duplicate checking.maxLength: Tracks the largest window size found.
Detailed Dry Run
| Step | L | R | Char | Action | Set Content | Max Len |
|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 'a' | Add 'a' | {a} | 1 |
| 2 | 0 | 1 | 'b' | Add 'b' | {a, b} | 2 |
| 3 | 0 | 2 | 'c' | Add 'c' | {a, b, c} | 3 |
| 4 | 0 | 3 | 'a' | Dupe 'a'! Shrink L | {b, c, a} | 3 |
| 5 | 1 | 4 | 'b' | Dupe 'b'! Shrink L | {c, a, b} | 3 |
⚠️ Common Pitfalls & Tips
Be careful with the window shrink condition. It must be a while loop to remove all characters until the duplicate is gone. Also, remember to add the current character after the shrink loop.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.