Skip to content

visual walkthrough

Minimum Window Substring

HardVariable Window + Frequency MapReported at: MetaAmazonMicrosoft+6

The flagship Hard window problem; a Meta and Google classic.

Solve on LeetCode

The idea

Expand the right edge until the window contains every needed letter with enough copies. Then shrink from the left while it still does; every time it is valid, remember it if it is the shortest.

Both edges only move forward, so the whole scan is linear.

Complexity

approachtimespace
Try every substringO(n² · m)O(m)
Sliding windowO(n + m)O(m)

More walkthroughs