0

Sliding window: variable size

Patternsused in ~100% of apps

The single highest-return pattern in this tier. Nobody tells you how wide the window is. A rule does. The window grows greedily and shrinks only when it has to, and a whole family of questions that look unrelated turn out to be this one.

Core idea. Push the right edge out one step at a time, always. After each step, while the window breaks the rule, pull the left edge in. Every position enters once and leaves once, so the whole thing is one pass even though there are two loops.
Playground, this isn't a screenshot
longest stretch with no repeated letter. "Abcabb"
a
b
c
a
b
b
a. Valid, length 1
a
b
c
a
b
b
ab. Valid, length 2
a
b
c
a
b
b
abc. Valid, length 3 ← best
a
b
c
a
b
b
abca. 'a' repeats, shrink
a
b
c
a
b
b
bca. Valid again, length 3
a
b
c
a
b
b
cab. Valid, length 3
a
b
c
a
b
b
bb → shrink to 'b', length 1

Neither edge ever moves left. Six characters, ten edge-movements in total, not thirty-six.

↑ play a little first, then dig in