Coding interview problems āŗ Sliding Window
šŖ Sliding Window Hard
Minimum Window Substring
Minimum Window Substring explained: expand the window until it covers every needed character, then shrink it from the left. O(n) sliding window, tested Python.
Also searched as: smallest window containing all characters Ā· minimum window substring sliding window
Try it live Free
Sliding Window
Watch the window add the element entering and drop the one leaving. In this problem the window stretches until it covers t, then contracts from the left, and the smallest valid stretch is the answer.
Also useful: Hash Tables
The problem
Given strings s and t, return the shortest substring of s that contains every character of t, counting duplicates. Return an empty string if no such window exists.
s has only one a.
The key insight
Expand the right edge until the window covers t, then shrink the left edge as far as it can go while still covering it, recording the smallest window. A counter of how many characters are still missing lets you know in O(1) whether the window covers t.
Step-by-step approach
- Count the characters of
tinneedand setmissing = len(t). - Move
rightacrosss; ifneed[ch] > 0, decrementmissing. Always decrementneed[ch](it can go negative for extras). - While
missing == 0: record the window if it is the smallest; then give backs[left]by incrementingneed; if that count becomes positive,missinggoes up by 1; moveleft. - Return the smallest recorded window, or an empty string.
Python solution
from collections import Counter
def min_window(s, t):
if not t or not s:
return ""
need = Counter(t)
missing = len(t)
left = 0
best = (float("inf"), 0, 0) # (length, start, end)
for right, ch in enumerate(s):
if need[ch] > 0:
missing -= 1
need[ch] -= 1 # negative = extra copies
while missing == 0: # window covers t: shrink it
if right - left + 1 < best[0]:
best = (right - left + 1, left, right + 1)
need[s[left]] += 1
if need[s[left]] > 0:
missing += 1
left += 1
return "" if best[0] == float("inf") else s[best[1]:best[2]]Complexity
Each index of s enters and leaves the window once, and t is counted once. The counter holds at most k distinct characters.
Common mistakes
- Treating
tas a set and ignoring duplicates, sot = "aa"matches a window with onea. - Recomputing whether the window covers
tby comparing whole dictionaries every step, which costs O(k) per step. - Recording the window after shrinking past validity, which reports a window that is one character too short.
Frequently asked questions
What is the time and space complexity of Minimum Window Substring?
O(n + m) time where n = len(s) and m = len(t), since each index of s enters and leaves the window once. Space is O(k) for the character counts.
Why can need counts go negative?
A negative count means the window has more copies of that character than t needs. Those extras can be dropped from the left without breaking coverage.
What does the missing counter track?
How many characters of t, counting duplicates, are still not covered by the window. When it reaches 0 the window is valid.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Sliding Window Sliding Window lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.