Longest substring without repeating characters
A window with two edges and a map of where each character was last seen. Rune-aware, so it counts characters, not bytes.
By devskills editorial team · Last updated
The problem
Given a string, return the length of the longest substring with no repeated character. "abcabcbb" gives 3 ("abc").
Brute force first
Try every start i. Extend j to the right with a fresh map[rune]bool, and stop at the first repeat. Track the longest.
O(n²) time (O(n³) if you recheck each substring from scratch), O(k) space. The window never throws away work it has already done.
The approach
Keep a window [start, i] with no repeats. Move i forward one character at a time. If that character was last seen inside the window, move start just past it. The window only ever moves right, so this is O(n) time, and O(k) space for k distinct characters.
package mainimport "fmt"// longestUnique returns the length, in characters, of the longest substring// with no repeated character. It works on runes, so "héllo" counts é once.func longestUnique(s string) int { last := make(map[rune]int) // rune -> index of its most recent position best, start := 0, 0 for i, r := range []rune(s) { if j, ok := last[r]; ok && j >= start { start = j + 1 // jump the left edge past the earlier copy } last[r] = i best = max(best, i-start+1) } return best}func main() { fmt.Println(longestUnique("abcabcbb")) // 3 (abc) fmt.Println(longestUnique("bbbbb")) // 1 fmt.Println(longestUnique("pwwkew")) // 3 (wke) fmt.Println(longestUnique("héllo")) // 3 (hél) fmt.Println(longestUnique("")) // 0}Dry run
"abcabcbb", window [start, i]:
| i | char | last seen | start | window | best |
|---|---|---|---|---|---|
| 0 | a | - | 0 | a | 1 |
| 1 | b | - | 0 | ab | 2 |
| 2 | c | - | 0 | abc | 3 |
| 3 | a | 0 (in window) | 1 | bca | 3 |
| 4 | b | 1 (in window) | 2 | cab | 3 |
| 5 | c | 2 (in window) | 3 | abc | 3 |
| 6 | b | 4 (in window) | 5 | cb | 3 |
| 7 | b | 6 (in window) | 7 | b | 3 |
Go-specific gotchas
- `range` over a string gives byte offsets.
for i, r := range sjumpsiby 2 foré. Here we range over[]rune(s)soicounts characters, which makesi-start+1a length in characters. - Check `j >= start`, not just that the rune was seen. A copy from before the window must not move
startbackwards. - Comma-ok again. Index 0 is a real position, so
last[r] == 0cannot mean "not seen". - For ASCII-only input, a
[128]intarray is faster than a map. Say it as an optimisation, after it works.
The array fast path, and the same window with a different rule
When the input is bytes (or you know it is ASCII), a [256]int replaces the map: no hashing and no allocation. Storing i + 1 instead of i lets the zero value mean "never seen", so you do not need a comma-ok lookup. It counts bytes, so it gives the wrong answer for text like "héé"; that is the trade you are making, and worth saying out loud.
The window itself is a template. Only the rule for "the window is invalid" changes. Here it is "more than k zeros", which solves the longest run of 1s if you may flip at most k zeros.
package mainimport "fmt"// longestBytes is the array version: last[c] holds the index after the// last sighting of byte c, so 0 can mean "never seen". It counts bytes,// which equals characters only for ASCII input.func longestBytes(s string) int { var last [256]int best, start := 0, 0 for i := 0; i < len(s); i++ { c := s[i] if last[c] > start { start = last[c] } last[c] = i + 1 if i-start+1 > best { best = i - start + 1 } } return best}// longestOnes is the same window with a different rule: the longest run of// 1s if you may flip at most k zeros. Grow on the right; when the window// holds more than k zeros, shrink from the left.func longestOnes(nums []int, k int) int { best, start, zeros := 0, 0, 0 for i, v := range nums { if v == 0 { zeros++ } for zeros > k { if nums[start] == 0 { zeros-- } start++ } if i-start+1 > best { best = i - start + 1 } } return best}func main() { fmt.Println(longestBytes("abcabcbb"), longestBytes("pwwkew"), longestBytes("")) fmt.Println(longestOnes([]int{1, 1, 0, 0, 1, 1, 1, 0, 1}, 1)) fmt.Println(longestBytes("héé")) // 3, not 2: it counts bytes, and "é" is two bytes}Complexity
| Approach | Time | Extra space |
|---|---|---|
| Every start, extend to the right | O(n²) | O(k) |
Window with map[rune]int, over []rune(s) | O(n) | O(n) for the rune slice, O(k) for the map |
Window with [256]int, over bytes | O(n) | O(1), a fixed 2 KB array |
Pitfalls
- `[]rune(s)` copies the string into a new slice of 4-byte runes. That is O(n) extra memory; fine in an interview, worth mentioning if they ask about very long input.
- Shrink with `for`, not `if`, when one step can make the window invalid by more than one unit, as in the
kzeros version. - Update `best` after fixing the window, not before, or you record the length of an invalid window.
- Write a rune-aware sliding window for the longest unique substring.
- Explain why the j >= start check is needed.
- Predict start and best at each step for a small string.
- Two edges; the right one always moves, the left one only jumps forward.
- Map from character to last index.
- Ranging over a string gives byte offsets; convert to
[]runewhen you need character positions.
longestUnique(s string) int that returns the length, in characters, of the longest substring with no repeated character. Example: "abcabcbb" → 3, "héllo" → 3.Tab indents. Press Esc, then Tab, to leave the editor.
- 1The main task: Find the length of the longest substring with at most two distinct characters. Shrink the window from the left when a third one arrives.
- 2Dry run
"abba". Ati = 3theawas last seen at index 0. What isstart, and why does thej >= startcheck matter here? - 3Find the maximum sum of any
kconsecutive numbers ([2 1 5 1 3 2], k = 3 → 9).