Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters is LeetCode 3 (Medium). Given a string s, return the length of the longest substring in which no character appears twice.
- A substring is a contiguous run of
s. Picking characters with gaps gives a subsequence, which does not count. shas up to 50,000 characters: letters, digits, symbols and spaces. Checking every substring is far too slow.
- 0 <= s.length <= 5 * 10⁴
- s consists of English letters, digits, symbols and spaces.
Intuition
Keep a window with no repeats and push its right end forward. The window breaks only when the new character already has a copy inside it, and at that point every window that still contains the older copy is broken too. So the left end can jump straight past the older copy in one move, instead of crawling forward one step at a time.
To make the jump O(1), store the last index where each character appeared. The stored index is only worth acting on when it lies inside the current window.
"Longest contiguous stretch with no duplicates" is a two-pointer window. When you can store where the offending item sits, move the left end straight past it instead of stepping.
Approach
Before reading on: when a character repeats, which window starts can still be part of an answer? Then decide what you need to store to skip the rest at once. Aim for O(n).
Two ways to move the left end
Store each character's last index and move left past a repeat in one step.
- One move per repeat: no inner loop.
- Reads each character once.
Keep the window's characters in a set and remove from the left until the repeat is gone.
- Easiest to get right: no stale indices to think about.
- Up to 2n steps: each character is added once and removed once.
Both run in O(n). The jump version is traced below; the set version follows the demo.
Remember where each character was last seen
Keep last, a map from character to the latest index where it appeared, and left, where the current window starts. Read s once from left to right.
Jump past a repeat that is inside the window
If s[right] was last seen at an index >= left, set left to that index plus one. Every start at or before the older copy would hold both copies, so all of them are skipped in one move.
Record the character, then the length
Set last[s[right]] = right, then update best with right - left + 1. After the jump, the characters from left to right are all different, so this length is a real answer.
Code in Python, C++ and Java
Set of the window, step
The set holds exactly the characters inside the window. When the next character is already in it, remove s[left] and step left until that copy is gone, then add the new one.
Common pitfalls
Jumping back to an index outside the window
if ch in last:
left = last[ch] + 1if last.get(ch, -1) >= left:
left = last[ch] + 1last keeps characters that already left the window. On "abba", the final a was last seen at 0 while left is 2, so the wrong code pulls left back to 1, lets the two bs in again, and returns 3. The answer is 2.
Restarting the window at the repeat
if last.get(ch, -1) >= left:
left = rightif last.get(ch, -1) >= left:
left = last[ch] + 1Characters between the older copy and right can stay. On "dvdf" the second d should keep "vd" and grow to "vdf" (3); restarting at the d gives "df" and returns 2.
Edge cases
The loop never runs and best stays 0, which the constraints allow.
A space is a character like any other, so the second space ends the window and the answer is 3 ("a b" or "b c"). The map does not care what kind of character it holds.
Complexity
left only moves forward. m is the number of possible characters (at most 128 here), since last holds one entry per character, not per position.Longest Substring Without Repeating Characters FAQ
How is this different from Longest Substring with At Most K Distinct Characters?
There a character may repeat freely and k caps how many different characters the window holds. Here every character may appear once, so the window needs no counts, only each character's last index.
Can an array replace the map?
Yes. The characters are ASCII, so an array of 128 ints filled with -1 does the same job: last[ord(ch)] instead of last.get(ch, -1). It avoids hashing and is often faster.