Pattern this teaches:Sliding Window
Minimum Window Substring
A hard Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Strings
- Sheets
- 2
- Core for
- 14 roles
The problem
Given two strings s and t, find the minimum-length substring of s that contains all characters of t including duplicates.
Example 1
- Input
- s="ADOBECODEBANC", t="ABC"
- Output
- "BANC"
Example 2
- Input
- s="a", t="a"
- Output
- "a"
Example 3
- Input
- s="a", t="aa"
- Output
- ""
Constraints
- 1 <= s.length, t.length <= 10^5
- s and t consist of uppercase and lowercase English letters
How to think about it
Updated 2026-09-29Do not track individual character matches independently. Count how many distinct required characters currently satisfy their needed frequency inside the window. Once that match counter equals the number of distinct characters in t, the window is complete; now push the left edge inward until dropping a critical character breaks the condition.
Approaches, worst first
Enumerate substrings
time O(n^2) · space O(1)
Generate every substring of s and verify if its character frequency counts cover all character demands from t. Checking each candidate window takes time proportional to the alphabet size or window length, blowing up quickly on long inputs.
Two-pointer sliding windowWrite this one
time O(n + m) · space O(1)
Expand right until all character quota demands are satisfied, then contract left to find the tightest boundary while keeping all quotas satisfied. Record the minimum length seen. Both pointers move strictly forward from start to end.
Where people lose marks · 3
- Returning an empty string instead of the shortest match when t is longer than s or impossible to satisfy. The initial answer trackers must detect when no valid window was ever formed.
- Mixed case letters collide if mapped to a length-26 array. Letters span both uppercase and lowercase ASCII ranges, requiring a 128-element table or a hash map.
- Counting total characters rather than distinct completed requirements. Redundant duplicates of one character must not falsely inflate the satisfaction count for missing ones.
The theory behind it
Strings — the ground this problem stands on. All Strings problems
What Strings is
A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.
When to reach for it
Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.
How the pattern works
Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.
More Strings problems
Target Roles
Core requirement for 14 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .