Minimum Window Substring
Problem Statement
You are given two strings s and t.
Find the shortest substring of s that contains every character of t, including repeats. For example, if t = "AAB", the substring needs at least two 'A' and one 'B'. Other characters may also appear in it.
Return that substring. If no such substring exists, return the empty string "".
The test data guarantees the answer is unique.
Two strings s and t of uppercase and lowercase English letters.
The shortest substring of s that covers all characters of t (with counts), or "" if none exists.
"BANC" has an A, a B and a C. No shorter substring has all three.
The whole string is the answer.
t needs two 'a' but s has only one, so no substring works.
- • 1 ≤ s.length, t.length ≤ 10^5
- • s and t contain only uppercase and lowercase English letters
- • Follow-up: can you do it in O(|s| + |t|) time?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, grow right and check all 128 counts after every step
Grow right; while the window covers t (checked over 128 chars), record and shrink
Track how many characters of t are still missing; covered when missing == 0
Try every start. Grow the end one character at a time and keep counts for the window. After each step, compare the window's counts with t's counts for all 128 characters. The first end where the window covers t gives the shortest window for that start; stop there and move to the next start.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.