Sliding window

A window is a range s[left:right + 1] that you grow on the right and shrink on the left. Each index enters once and leaves once, so the scan is O(n) instead of checking every substring.

The signal: "longest" or "shortest" contiguous run that satisfies a rule.

For the longest substring with no repeated character, keep a dict from each character to the last index you saw it. When the new character was seen inside the window, jump left past that old position. The window is then valid again, and its length is right - left + 1.

A common mistake: moving left backwards. If the old position is before left, it is outside the window and you must ignore it.

Exercise: finish longest_unique so it returns the length of the longest substring of s with no repeated characters. It must be O(n): the speed test uses a string where every character is different. Press Check to run the tests.