↶ Home

Sliding Window.

— Longest Substring Without Repeating. Step through the window growing on the right and shrinking on the left.
in window
about to add
duplicate detected
Step 0 /
Action
Press Next → to start.
Pointers
L=0, R=0
Window: empty
Set (chars in window)
— empty —
Longest valid window so far
0
none yet

The takeaway.

R walks forward through every character exactly once. When adding the next character would create a duplicate, L shrinks from the left — removing characters until the duplicate is gone. Then add the new character.

Each character is visited at most twice — once by R (when added), once by L (when removed). That's O(n) time. Both pointers move FORWARD; they never converge.

This is what makes sliding window distinct from converging two-pointer — sliding window is for "longest/shortest contiguous range with some property," converging is for "find a pair in a sorted array."

SEE ALSO → Flashcards · sliding-window category · Cheatsheet · pattern 3 · Practice script · LC 3