Problem 10 · Two Pointers & Sliding Window
Minimum Window Substring
01Problem
Given strings s and t, return the smallest substring of s that contains every character of t (with multiplicity), or an empty string.
02Key insight
Expand right until the window satisfies t, then shrink left while it still does. Track a single 'missing' counter so validity checks are O(1) rather than comparing maps.
03Solution
Try it first.
TimeO(|s| + |t|)
SpaceO(alphabet)
tests.py · 4 passing
tests.pypython4 lines
assert min_window("ADOBECODEBANC", "ABC") == "BANC"
assert min_window("a", "a") == "a"
assert min_window("a", "aa") == ""
assert min_window("aa", "aa") == "aa"04Staff-level follow-up
If t is small but s is huge and only few chars matter? Pre-filter s to (index, char) pairs for chars in t and slide over that list, cutting work when s is mostly irrelevant characters.