Minimum Window Substring
Find the shortest substring containing every required character count.
Why does this pattern fit?
Restate the exact job
Find the shortest substring containing every required character count.
A window becomes valid when all required kinds reach their needed counts.
O(n + m) time · O(k) space
Counting distinct characters instead of required multiplicities accepts invalid windows.
How to solve Minimum Window Substring
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Find the shortest substring containing every required character count.
Why Sliding window fits
A window becomes valid when all required kinds reach their needed counts.
State to maintain
Need counts, window counts, formed kinds, and two boundaries.
Transition
Expand until valid, then shrink while valid and record the smallest range.
Time and space
O(n + m) time · O(k) space
Counterexample to the tempting mistake
Counting distinct characters instead of required multiplicities accepts invalid windows.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.