Longest Substring Without Repeating Characters
Scratchpad
Saved in this browser
Problem
Section titled “Problem”Given a string s, find the length of the longest substring without duplicate characters.
Example 1:
Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Note that "bca" and "cab" are also correct answers.Example 2:
Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:
Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.Constraints:
-
s.length sconsists of English letters, digits, symbols and spaces.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Keep a window s[left .. right] that never contains a repeated character, and grow it one character at a time on the right. When the new character c = s[right] already occurs inside the window, every window that contains both copies is invalid, so the left edge must move just past the earlier copy. Nothing to the left of that copy can be part of a valid window ending at right, so we never need to move left backwards.
Instead of shrinking the window one step at a time, remember last[c], the most recent index of every character. Then left can jump straight to last[c] + 1 — but only if that copy is inside the window (last[c] >= left); an older copy to the left of the window is irrelevant.
Every window s[left .. right] is the longest valid substring ending at right, so the maximum of their lengths is the answer.
Algorithm
Section titled “Algorithm”lastis an array of 256 entries, all-1;left = 0,best = 0.- For each
rightfrom0ton - 1, withc = (unsigned char)s[right]:- If
last[c] >= left, setleft = last[c] + 1. - Set
last[c] = right. - Update
best = max(best, right - left + 1).
- If
- Return
best.
Walkthrough
Section titled “Walkthrough”The blue cells are the current window; orange marks the earlier copy that forces left to jump:
Complexity
Section titled “Complexity”- Time: —
rightvisits each index once and all other work per step is . - Space: — a fixed array of 256 indices, independent of
n.
Edge cases
Section titled “Edge cases”- Empty string: the loop never runs and
bestis0. - All characters equal (Example 2):
leftjumps torightevery step, so the window length stays1. - Old copy outside the window: in
"abba", at the finalaits previous index0is left ofleft = 2, so thelast[c] >= leftcheck keepsleftfrom moving backwards. - Characters outside 0-127: the cast to
unsigned charkeeps the array index non-negative.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int lengthOfLongestSubstring(string s) { vector<int> last(256, -1); // last index of each character int left = 0, best = 0; for (int right = 0; right < (int)s.size(); right++) { unsigned char c = s[right]; if (last[c] >= left) left = last[c] + 1; last[c] = right; best = max(best, right - left + 1); } return best; }};class Solution { public int lengthOfLongestSubstring(String s) { int[] last = new int[256]; // last index of each character Arrays.fill(last, -1); int left = 0, best = 0; for (int right = 0; right < s.length(); right++) { char c = s.charAt(right); if (last[c] >= left) left = last[c] + 1; last[c] = right; best = Math.max(best, right - left + 1); } return best; }}class Solution: def lengthOfLongestSubstring(self, s: str) -> int: last = {} # last index of each character left = best = 0 for right, c in enumerate(s): if last.get(c, -1) >= left: left = last[c] + 1 last[c] = right best = max(best, right - left + 1) return bestGive it a try first
Attempt the problem on LeetCode — the code will still be here.









