A
D
B
A
N
C
[0][1][2][3][4][5]
Expand + contract window
▸1build need from t; required ← distinct chars in t2have ← 0; window counts ← {}3left ← 0; best ← none4for right ← 0 to n − 1:5 add s[right] to window; if it now meets need: have++6 while have == required: // window is valid7 if width < best: best ← [left, right]8 remove s[left]; if it drops below need: have--9 left++10return best window
state
- s"ADBANC"
- t"ABC"
- n6