Practice Discrete Math

Algorithms / Sliding Window Strings

Least You Need to Know: Two Pointers, Sliding Windows, and String Scans

Many interview string problems are not about deep string theory. They are about keeping a **contiguous window** consistent while scanning left to right. The key question is what summary of the current window lets you expand, shrink, and answer the prompt in overall linear time.

The least you need to know

Key notation

[L, R] current window from left index L to right index R
freq[c] count of character c inside the current window
invariant condition that the maintained window must satisfy

Tiny worked example

  • Suppose the task asks for the longest substring with no repeated characters.
  • Expand `R` one character at a time and update `freq`.
  • If a duplicate appears, move `L` rightward until the duplicate is removed.
  • After each repair step, the window again satisfies the invariant "all characters are unique," so its length is a valid candidate answer.

Common mistakes

How to recognize this kind of problem

Start practice