Problem 3 · LeetCode 3(opens in a new tab)
Longest Substring Without Repeating Characters
MediumHash tables
You are given a string s. Look at every run of consecutive characters in it and find the longest run in which no character shows up twice. Return the length of that run.
The run must be contiguous: you cannot skip characters in the middle. Letters, digits, symbols and spaces all count as characters, and upper and lower case are different. An empty string has an answer of 0.
Example 1
Input: s = "xyzyab" Output: 4 Why: "zyab" has no repeats; any longer run repeats y
2 more examples and the constraints
Example 2
Input: s = "zzzz" Output: 1 Why: every run longer than one has two z's
Example 3
Input: s = "go go" Output: 3 Why: "go " works; the space is a character too
Constraints
- 0 ≤ length of s ≤ 100,000
- s holds English letters, digits, symbols and spaces (printable ASCII)
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Every start, scan forwardO(n²)
- Sliding window with a setO(n)
- Sliding window, jump on repeatO(n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 100,000 characters | Verdict |
|---|---|---|---|
| Every start, scan forward | O(n²) | Mention it, then move on | |
| Sliding window with a set | O(n) | Passes; up to two steps per character | |
| Sliding window, jump on repeat | O(n) | Best; one step per character |
n = length of s, k = number of possible characters (about 95 printable ones).
Edge cases to test
- Empty string. "" has no characters, so the answer is 0. The loop never runs; make sure best starts at 0.
- All the same character. "zzzz" gives 1: every step the window shrinks back to one character.
- Old copy outside the window. In "abba", the last a was seen at index 0, but left is already 2. Do not move left back to 1; the answer is 2.
- Spaces and symbols. "!@ #!" gives 4. A space is a character like any other.
Common mistakes
- Setting left = last[c] + 1 without checking it is inside the window, which can move left backwards ("abba" would give 3).
- Answering with the longest subsequence instead of a contiguous run, skipping characters in the middle.
- Measuring the window as right − left instead of right − left + 1.
How to talk through it
- 1Clarify. Is it the length or the run itself? Can the string be empty? Which characters can appear, and is case significant?
- 2Give the brute force. Every start, scan until a repeat. Say it re-reads the same characters and is O(n²) in general.
- 3Name the window. A run without repeats stays valid if you drop characters from its left, so a window that only moves forward works.
- 4Code the jump. Store the last index of each character and jump left past a repeat, but only if that copy is inside the window.
- 5Test out loud. Run "abba", the empty string and an all-same string before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?