awelgorithms

Decide fixed versus variable window length

Updated

Fixed length: k or t.length is given; slide by paired include/exclude. Variable length: expand until a constraint says stop, shrink while it says shrink; length is discovered.

What to focus on

Anagram windows are fixed. Longest unique, covering, at-most-k-distinct, product-under-limit are variable. Do not shrink a fixed-k window except the one paired drop.

Worked examples

fixed anagram

s = "cbaebabacd", t = "abc"
  1. m=3 always
  2. add one drop one

Result: fixed length 3

variable unique

s = "abcabcbb"
  1. length 1,2,3 then shrink
  2. best 3 is discovered

Result: variable

fixed k vs variable sum

a = [4, 1, 9, 1]
  1. exactly length 2: three windows
  2. longest sum ≤ 10: length changes with shrink

Result: fixed 2 vs variable

Try it

Write classifyLength(s). s is a string, or a is an array of integers. Fixed length means k or t.length is given. Variable length is discovered by expand and shrink. Print fixed or variable for each job. Return nothing. Label cbaebabacd vs abc, unique "abcabcbb", length-2 on [4, 1, 9, 1], covering ADOBECODEBANC vs ABC.

s and t are nonempty with t.length ≤ s.length. a is nonempty with 1 ≤ k ≤ n.