Minimum Window Substring
Problem
You are given two strings, a source s and a pattern t. Find the shortest contiguous substring of s that contains every character of t with at least the multiplicity it has in t (so if t has two As, the window needs two As). Return that substring, or an empty string if no such window exists. If several windows tie for the shortest length, return the leftmost one.
shas at most 100,000 characters;thas at least one and at most as many ass.- Both strings are made of English letters, either case.
Examples
s = "XQYAZQBYXA", t = "AQX"→"XQYA". The first four characters already cover oneX, oneQ, and oneA; no shorter span does.s = "timetable", t = "ttb"→"timetab". Twots are required, so the window must stretch from the firsttto theb.s = "zzz", t = "zzzz"→"". Fourzs are needed and only three exist.
Hints
- If a window contains everything in
t, does extending it to the right ever break that? Does trimming it from the left? - Track how many characters of
tare currently satisfied, so you can tell in O(1) whether the window is valid without rescanning counts. - Expand
hiuntil the window is valid, then contractlowhile it stays valid, recording the best; aneedmap plus a count of fully satisfied characters drives both moves.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.