awelgorithms

Draw left and right after each window move

Updated

After every expand (R++) or shrink (L++), write the pair (L,R) and the length R-L+1. The drawing is the algorithm's cursor, not a later reconstruction.

What to focus on

One line per move. Expand changes R only. Shrink changes L only. If you skip a print, you cannot audit the next state.

Worked examples

unique abca

s = "abca"
  1. R=0 (0,0)
  2. R=1 (0,1)
  3. R=2 (0,2)
  4. R=3 shrink L=1 (1,3)

Result: (0,0) (0,1) (0,2) (1,3)

sum slide

a = [4, 1, 9, 1], length 3
  1. seed (0,2)
  2. slide drop+add (1,3)

Result: (0,2) then (1,3)

zeros shrink

a = [1, 0, 0, 1, 9], k=1, at R=2
  1. invalid (0,2)
  2. shrink (1,2) still 2 zeros
  3. shrink (2,2)

Result: (0,2) → (1,2) → (2,2)

Try it

Write drawLR(s). s is a string, or a is an array of integers. L is the left index. R is the right index. Expand changes R only. Shrink changes L only. The window includes s[L..R]. Print (L, R) after every move. Return nothing. Unique "abca", length-3 on [4, 1, 9, 1], and k=1 zeros on [1, 0, 0, 1, 9] at the second 0.

a (or s) is nonempty. 1 ≤ k ≤ n for the fixed-length slide.