Longest Substring Without Repeating Characters
Return the length of the longest substring of s with no repeating characters.
Intuition
Keep a window that holds only distinct characters. As the right edge moves forward, if it hits a character already inside the window, jump the left edge just past that character's previous spot. The window never shrinks needlessly.
Approach
Checking every substring is quadratic
There are O(n²) substrings, and verifying each for distinct characters re-scans constantly. But the substrings overlap enormously — extending a known-good window by one character barely changes anything. That overlap is the cue for a sliding window: maintain one contiguous range and adjust its edges instead of rebuilding from scratch.
Grow the right edge, jump the left edge past repeats
Keep a window [left, right] that contains only distinct characters. Push right forward one character at a time. The only thing that can break the window is encountering a character already inside it — when that happens, we must move left just past that character's previous position so the duplicate falls out of the window. To know where that was, store each character's most recent index in a map.
Guard against stale positions; track the best
When s[right] was last seen at some index, only jump left if that index is ≥ left — otherwise the earlier occurrence is already outside the window and left must not move backward. After adjusting, record last[ch] = right and update best = max(best, right − left + 1). Each index enters and leaves the window once, so it's a single O(n) pass; space is bounded by the character set.
Solution & live demo
Edge cases
Each new character forces left to follow right, so the window length stays 1.
The loop never runs; best stays 0.
The last[ch] >= left guard ignores stale occurrences, so left never moves backward.