mediumStringHash TableSliding WindowSliding Window

Longest Substring Without Repeating Characters

mediumTime: O(n)Space: O(min(n, m))

Recognize the pattern

longest substringno repeating charactersvariable-size window

Brute force idea

A straightforward first read of Longest Substring Without Repeating Characters is this: Check every substring for unique characters — or. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see Longest Substring Without Repeating Characters is this: Sliding window with a set: expand right, shrink left when duplicate found. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Longest Substring Without Repeating Characters is this: The window always contains unique characters — shrink left until the duplicate is removed. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

One easy way to drift off course in Longest Substring Without Repeating Characters is this: Not shrinking the window properly — remove characters from the set as you move left. The fix is usually to return to the meaning of each move, not just the steps themselves.

Sliding Window Pattern