Longest Substring Without Repeating Characters
On this page
Problem statement
Given a string, return the length of the longest substring that contains no repeated character. A substring is a contiguous run of characters, so "pwke" picked from "pwwkew" doesn't count.
This is the textbook variable-size sliding window: grow the window on the right, and when a rule breaks, move the left edge just far enough to fix it. Original problem: LeetCode 3.
Example

Input: "abcabcbb"
Output: 3 # "abc"
Input: "pwwkew"
Output: 3 # "wke"; "pwke" is not contiguous
Constraints (assumed)
- 0 ≤ length ≤ 50,000.
- Letters, digits, symbols and spaces.
What this question tests
- Recognizing a variable-size sliding window ("longest substring such that…").
- Moving the left edge correctly and only forward.
- Using a hash map (or a 128-slot array) to remember positions.
Questions to ask first
- What characters can appear? ASCII lets you use an array of 128 instead of a map. (Here: any printable ASCII.)
- Length or the substring itself? (Here: the length; returning the substring needs the start index too.)
- Is the empty string valid input? (Here: yes, answer 0.)
- Case sensitive? (Here: yes, 'a' and 'A' differ.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.