Sliding window
Reuse work across adjacent ranges.
A window is a contiguous interval whose state can be extended and repaired incrementally.
Mechanism and state
Choose the state and objective before the pointer rule. A longest feasible window such as unique symbols or at-most-k zeros expands right and repairs violations by moving left. A minimum covering window expands until coverage holds, then shrinks while still covered. A fixed-size permutation window enters one symbol and evicts the oldest once its width is exceeded, comparing full multiplicities. A maximum-value window keeps undominated indices in a deque rather than a single running maximum.
Why the pattern works
For an exact-state window, explain what each counter means, when the current interval is feasible, and why discarded starts cannot improve the result later. Both boundaries move forward, so a nested contraction loop can still total O(n). Historical-frequency Character Replacement is a deliberate exception: its retained interval can be invalid. Its length-only result needs a separate proof that every new record length has a feasible witness and that no optimum is discarded; the usual current-validity invariant must not be asserted there.
Work through an example
On "abba", unique-symbol windows are "a", "ab", "b", "ba". The final a previously occurred at 0, before left=2; max(left,old+1) keeps left at 2. For minimum coverage, "AABC" covering "ABC" may remove its surplus first A while staying covered, but removing the second A breaks coverage. For Character Replacement on "AABABBA", k=1, an earlier length-four window is feasible even when a later retained "ABAB" has actual cost two and historical cost one.
Where the reasoning fails
Do not copy a pointer template without its predicate: longest feasible windows shrink on invalidity, minimum coverage windows shrink on validity, and fixed-width windows enforce length first. Negative values break ordinary positive-sum contraction; arbitrary-sign exact sums use prefix frequencies and shortest threshold sums need a different argument. Declare ASCII, UTF-16 code units, code points, or graphemes explicitly, and count output storage separately.
Reconstruct the reasoning
Trace "tmmzuxt" for uniqueness, "AABC"/"ABC" for minimum coverage, and "aaaa"/"aa" for overlapping anagrams. For "AABABBA",k=1, record true and historical dominant counts and explain why a stale retained interval is not a witness. For a maximum deque, identify which indices expire and which are removed by newer domination.
Apply this foundation
- Longest Substring Without Repeating Characters · Explanation
- Longest Repeating Character Replacement · Explanation
- Permutation in String · Explanation
- Find All Anagrams in a String · Explanation
- Minimum Window Substring · Explanation
- Minimum Size Subarray Sum — Positive Inputs · Explanation
- Sliding Window Maximum · Explanation
- Longest Ones with at Most K Flips · Explanation
- Subarray Sum Equals K · Explanation
- Subarray Product Less Than K · Explanation