Skip to content

visual walkthrough

Longest Substring Without Repeating Characters

MediumVariable Window (Shrink When Invalid)Reported at: AmazonMicrosoftMeta+16

The canonical variable window; one of the most-asked questions anywhere.

Solve on LeetCode

The idea

The answer is the longest stretch with no repeated letter. Instead of restarting for every start position, keep one window and slide it: extend on the right, and when the new letter is already inside, drop letters from the left until it isn't.

Each letter enters and leaves the window at most once, so the whole scan is linear.

Complexity

approachtimespace
Try every startO(n²)O(n)
Sliding windowO(n)O(n)

More walkthroughs