PROBLEM 13 OF 75

Minimum Window Substring

Find the shortest substring containing every required character count.

PATTERNSliding windowExpand to learn; shrink to restore the rule.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find the shortest substring containing every required character count.

WHY THIS FITS

A window becomes valid when all required kinds reach their needed counts.

COMPLEXITY

O(n + m) time · O(k) space

COMMON FAILURE

Counting distinct characters instead of required multiplicities accepts invalid windows.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗