Skip to content

Longest Substring Without Repeating Characters

Solve on LeetCodeMedium
Time
Space

Given a string s, find the length of the longest substring without duplicate characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3. Note that "bca" and "cab" are also correct answers.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: 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:

  • 0≤0 \le s.length ≤105\le 10^5
  • s consists of English letters, digits, symbols and spaces.

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.

  1. last is an array of 256 entries, all -1; left = 0, best = 0.
  2. For each right from 0 to n - 1, with c = (unsigned char)s[right]:
    • If last[c] >= left, set left = last[c] + 1.
    • Set last[c] = right.
    • Update best = max(best, right - left + 1).
  3. Return best.

The blue cells are the current window; orange marks the earlier copy that forces left to jump:

  1. Sliding window on "abcabcbb", step 1 of 10
  2. Sliding window on "abcabcbb", step 2 of 10
  3. Sliding window on "abcabcbb", step 3 of 10
  4. Sliding window on "abcabcbb", step 4 of 10
  5. Sliding window on "abcabcbb", step 5 of 10
  6. Sliding window on "abcabcbb", step 6 of 10
  7. Sliding window on "abcabcbb", step 7 of 10
  8. Sliding window on "abcabcbb", step 8 of 10
  9. Sliding window on "abcabcbb", step 9 of 10
  10. Sliding window on "abcabcbb", step 10 of 10
10 steps. Swipe or scroll to move between them.
  • Time: O(n)O(n) — right visits each index once and all other work per step is O(1)O(1).
  • Space: O(1)O(1) — a fixed array of 256 indices, independent of n.
  • Empty string: the loop never runs and best is 0.
  • All characters equal (Example 2): left jumps to right every step, so the window length stays 1.
  • Old copy outside the window: in "abba", at the final a its previous index 0 is left of left = 2, so the last[c] >= left check keeps left from moving backwards.
  • Characters outside 0-127: the cast to unsigned char keeps 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 best

Give it a try first

Attempt the problem on LeetCode — the code will still be here.