Learn hub

Sliding Window — Live Range

Maintain [L,R] that satisfies a constraint; answer from length/sum/counts.

Suno.Stringdihai.Sabselambisubstringchahiyejismekoicharacterdobaarnaaaye.

Listen. You get a string. Find the longest substring with no repeated character.

0:00 / 1:02

When to use this

  • Longest/shortest subarray/substring
  • Fixed k
  • At most K zeros/distinct

C++ template

int L = 0;
for (int R = 0; R < n; R++) {
    // add s[R]
    while (/* invalid */) { /* remove s[L] */ L++; }
    // update answer
}

Dry run

"abcabcbb" → max unique window length 3.

Pitfalls

  • Answer update timing
  • R-L+1 off-by-one
  • Subsequence vs subarray

Say this in the interview

Each index enters/leaves once → O(n).

Problems in this topic (6)

Each opens teach-first (animated when available), then the code panel.