Longest Substring Without Repeating Characters
Find the length of the longest contiguous substring that contains no duplicate characters using a sliding window and last-seen table.
01The problem
Given a string s, find the length of the longest substring without repeating characters. A substring is a contiguous non-empty sequence of characters within a string.
| Input | Output | Why |
|---|---|---|
"abcabcbb" | 3 | The answer is "abc", with length 3 |
"bbbbb" | 1 | The answer is "b", with length 1 |
"pwwkew" | 3 | The answer is "wke", with length 3 ("pwke" is a subsequence, not a substring) |
"" | 0 | Empty string has length 0 |
Constraints: 0 to 50,000 characters, consisting of English letters, digits, symbols, and spaces.
02How to spot it
- Looking for the longest contiguous piece.
- The validity condition is: no repeated characters in the window.
- When a duplicate character is encountered at
right, we can jumpleftdirectly past the previous occurrence of that character instead of shrinking one step at a time.
So: variable-size sliding window with a last-seen index table.
03Pseudocode
last[c] = 0 for every char
left = 0 ; best = 0
for right from 0 to n - 1:
c = s[right]
left = max(left, last[c])
best = max(best, right - left + 1)
last[c] = right + 1
return best- Grow the window to the right by advancing
right. - If
cwas seen inside the current window, jumpleftpast its previous location. - Record the maximum window length seen, and store
right + 1as the new position forc.
04Watch it work
Initialize last table for characters. Window starts at left = 0, best = 0.
Pseudocode
- last[c] = 0 for every char
- left = 0 ; best = 0
- for right from 0 to n - 1:
- c = s[right]
- left = max(left, last[c])
- best = max(best, right - left + 1)
- last[c] = right + 1
- return best
Variables
- left
- 0
- best
- 0
05Step-by-step dry run
- 1a0b1c2a3b4c5b6b7leftright
Initialize last table for characters. Window starts at left = 0, best = 0.
left = 0best = 0
- 2a0b1c2a3b4c5b6b7leftright
Window "a" has length 1 with no duplicate characters. New best: 1.
left = 0right = 0len = 1best = 1
- 3a0b1c2a3b4c5b6b7leftright
Window "ab" has length 2 with no duplicate characters. New best: 2.
left = 0right = 1len = 2best = 2
- 4a0b1c2a3b4c5b6b7leftright
Window "abc" has length 3 with no duplicate characters. New best: 3.
left = 0right = 2len = 3best = 3
- 5a0b1c2a3b4c5b6b7leftright
'a' was previously seen inside the window (index 0). Jump left to 1 past the old copy.
c = aleft = 1right = 3best = 3
- 6a0b1c2a3b4c5b6b7leftright
Window "bca" has length 3 with no duplicate characters.
left = 1right = 3len = 3best = 3
- 7a0b1c2a3b4c5b6b7leftright
'b' was previously seen inside the window (index 1). Jump left to 2 past the old copy.
c = bleft = 2right = 4best = 3
- 8a0b1c2a3b4c5b6b7leftright
Window "cab" has length 3 with no duplicate characters.
left = 2right = 4len = 3best = 3
- 9a0b1c2a3b4c5b6b7leftright
'c' was previously seen inside the window (index 2). Jump left to 3 past the old copy.
c = cleft = 3right = 5best = 3
- 10a0b1c2a3b4c5b6b7leftright
Window "abc" has length 3 with no duplicate characters.
left = 3right = 5len = 3best = 3
- 11a0b1c2a3b4c5b6b7leftright
'b' was previously seen inside the window (index 4). Jump left to 5 past the old copy.
c = bleft = 5right = 6best = 3
- 12a0b1c2a3b4c5b6b7leftright
Window "cb" has length 2 with no duplicate characters.
left = 5right = 6len = 2best = 3
- 13a0b1c2a3b4c5b6b7leftright
'b' was previously seen inside the window (index 6). Jump left to 7 past the old copy.
c = bleft = 7right = 7best = 3
- 14a0b1c2a3b4c5b6b7leftright
Window "b" has length 1 with no duplicate characters.
left = 7right = 7len = 1best = 3
- 15a0b1c2a3b4c5b6b7
Longest substring without repeating characters has length 3.
best = 3
06Java solution
public class LongestSubstringWithoutRepeating {
public static int lengthOfLongestSubstring(String s) {
int[] last = new int[128];
int left = 0, best = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
left = Math.max(left, last[c]);
best = Math.max(best, right - left + 1);
last[c] = right + 1; // 1-based index: 0 means never seen
}
return best;
}
public static void main(String[] args) {
System.out.println(lengthOfLongestSubstring("abcabcbb"));
System.out.println(lengthOfLongestSubstring("bbbbb"));
System.out.println(lengthOfLongestSubstring("pwwkew"));
System.out.println(lengthOfLongestSubstring(""));
}
}3 1 3 0
Tricky lines, decoded
Math.max(left, last[c]): Preventsleftfrom ever moving backwards. In"abba", when the second'a'at index 3 is reached, its last position was index 0 (last['a'] = 1), butleftis already at 2 past the'b'. Taking the maximum keepsleftat 2.last[c] = right + 1: Stores index + 1 so that the default value 0 cleanly represents "never seen yet".
07Why it works
For each right, the longest valid window ending at right starts immediately after the latest previous occurrence of any character within the window. Storing the most recent position of each character in last[c] lets left jump past the duplicate in O(1). Because left only moves forward, every substring ending at right is evaluated without redundant re-checks.
Invariant: Substring s[left .. right] contains no repeating characters.
08Time and space
- Time: O(n). A single pass where
rightadvances from 0 to n - 1 andleftonly jumps forward. - Space: O(1). The ASCII table
int[128]uses fixed memory regardless of the string length n.
09Edge cases
| Input | Output | Why it matters |
|---|---|---|
"" | 0 | Empty string |
" " | 1 | A space is a valid character |
"abba" | 2 | left must not move backwards |
"dvdf" | 3 | Duplicate appears in the middle of a valid sequence |
"tmmzuxt" | 5 | First 't' was outside the active window |
10Common mistakes
- Moving
leftbackward. Writingleft = last[c]instead ofleft = Math.max(left, last[c]). On"abba", the second'a'would pullleftback from 2 to 1, returning an incorrect answer of 3. - Clearing the set. Using a
HashSetand callingset.clear()on repeat throws away valid, non-repeating characters between the old duplicate andright. - Using
int[26]. The problem constraints include spaces, digits, and punctuation symbols. An array of size 26 will cause anArrayIndexOutOfBoundsException. Useint[128](or a hash map for Unicode).
11Other approaches
| Approach | Time | Space | Use when |
|---|---|---|---|
| Check all substrings | O(n³) | O(n) | Never in an interview |
| Window + Set crawl | O(n) | O(k) | Easy to explain, shrinks one by one |
| Last-index jump | O(n) | O(1) | Default interview answer |
| HashMap last-index | O(n) | O(k) | Full Unicode or arbitrary charset |
12In the interview
- Return the substring itself? Track
bestStartandbestLenwheneverright - left + 1 > best. Returns.substring(bestStart, bestStart + bestLen). - Why is jumping O(n) overall? Because
leftonly moves forward, the sum of all jumps across the entire string is at most n.
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why do we write left = Math.max(left, last[c]) instead of left = last[c]?
2. What does storing right + 1 in the last array achieve?
14Solve it yourself
Try it on LeetCode #3.