Longest Substring Without Repeating Characters, Sliding Window
Longest Substring Without Repeating Characters (LeetCode 3): grow the window on the right, shrink it on the left at a repeat. O(n).
Longest Substring Without Repeating Characters (LeetCode 3) gives you a string and asks for the length of the longest stretch of it in which no character appears twice. Our example is one of LeetCode's own: "pwwkew". The answer is 3, from "wke". Note that "pwke" doesn't count: it skips a letter, so it is a subsequence, not a substring.
Checking every substring works, but a string of n letters has about n²/2 substrings, and checking each one for repeats costs more on top. A sliding window does the whole job in one pass.

What is the sliding window?
Picture a magnifying glass lying over the text. Whatever is under the glass is the current substring, and it never contains a repeat. The glass has two edges:
- the right edge moves one letter at a time, always forward;
- when the letter the right edge reaches is already under the glass, the left edge moves forward, dropping letters, until the old copy of that letter is gone.
A set holds the letters under the glass, so asking "is this letter already inside?" takes constant time on average. After each step, the glass's width, right - left + 1, is a candidate for the answer, and we keep the best.

How does the window move over "pwwkew"?
| right | Letter | Dropped from the left | Under the glass | Length | Best |
|---|---|---|---|---|---|
| 0 | p | p | 1 | 1 | |
| 1 | w | pw | 2 | 2 | |
| 2 | w | p, w | w | 1 | 2 |
| 3 | k | wk | 2 | 2 | |
| 4 | e | wke | 3 | 3 | |
| 5 | w | w | kew | 3 | 3 |
The second w is the interesting step. It is already in the set, so the left edge drops p; w is still inside, so it drops the old w too. Only then does the new w go in, and the glass is down to one letter.

Then the glass grows again over k and e. "wke" is three letters long, the best so far.

The last w repeats the one at the left edge, so the glass drops just that w and takes the new one: "kew", still three. The best stays 3.

What does the program look like?
This is the program from the video, line for line:
def longest(s):
window = set()
left = best = 0
for right, ch in enumerate(s):
while ch in window:
window.remove(s[left])
left += 1
window.add(ch)
best = max(best, right - left + 1)
return best
print(longest("pwwkew"))
Output:
3
We also checked it against a brute force that tries every substring, on 3,000 random strings of up to 12 letters: the two always agree. LeetCode's other examples give the expected answers too: "abcabcbb" returns 3 and the empty string returns 0.
Why is it O(n)?
The while loop inside the for loop looks like it could make this quadratic, but count what happens to each letter instead. Every letter is added to the set exactly once, when the right edge reaches it, and removed at most once, when the left edge passes it. Both edges only ever move forward. So over the whole run there are at most n additions and n removals: O(n) time. The set never holds more letters than the alphabet has, so the extra memory is O(min(n, alphabet size)).

Try it
Run the same function on five b's: longest("bbbbb"). Trace the glass by hand: how wide does it ever get, and what does the function return?