Minimum Window Substring, a Sliding Window with Counts
Minimum Window Substring (LeetCode 76): a Counter of what's still needed, grow on the right, shrink on the left while nothing is missing.
Minimum Window Substring (LeetCode 76) gives you a string s and a string t and asks for the shortest substring of s that contains every character of t, counting repeats. Our example is short enough to trace by hand: s = "AXBCAB", t = "ABC". The answer is "BCA", three letters long.
Checking every substring works, but a string of n letters has about n²/2 substrings, and counting the letters in each costs more on top. A sliding window with a table of counts does it in one pass.

What is the sliding window here?
Picture a train carriage running along the text. The letters under it are the current substring. Letters board at the front: the right edge moves forward one letter at a time. Letters leave at the back: the left edge moves forward to drop them.
Two values describe the carriage:
need, aCounterbuilt fromt: for each letter, how many more copies the carriage still needs. It starts as A:1, B:1, C:1. A letter that isn't wanted, like X, goes below zero when it boards, which is harmless.missing, the total still needed: it starts atlen(t), 3, and drops by one whenever a letter boards thatneedwas still waiting for.
When missing reaches 0, everything from t is on board. Then we record the carriage if it is the shortest yet, and shrink from the back for as long as missing stays 0. The program records the best carriage as two indices, lo and hi, so the answer is s[lo:hi]; it only cuts the string once, at the end.
How does the carriage move over "AXBCAB"?
| Step | Event | Under the carriage | need A B C | missing | s[lo:hi] |
|---|---|---|---|---|---|
| 1 | A, X, B, C board | AXBC | 0 0 0 | 0 | "AXBC" |
| 2 | A leaves | XBC | 1 0 0 | 1 | "AXBC" |
| 3 | A boards | XBCA | 0 0 0 | 0 | "AXBC" (4 is not shorter) |
| 4 | X leaves | BCA | 0 0 0 | 0 | "BCA" |
| 5 | B leaves | CA | 0 1 0 | 1 | "BCA" |
| 6 | B boards | CAB | 0 0 0 | 0 | "BCA" (3 is not shorter) |
| 7 | C leaves | AB | 0 0 1 | 1 | "BCA" |
After A, X, B and C board, nothing is missing, so the first full window is "AXBC", four letters, and lo, hi become 0, 4.
![The carriage covers A, X, B, C in teal; missing 0, s[lo:hi] = "AXBC", need A:0 B:0 C:0, and lo, hi = left, right + 1 highlighted in the code](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/alg-011-minimum-window/full.jpg)
Now the carriage shrinks from the back. A leaves, need["A"] goes back up to 1, and missing becomes 1, so the shrinking stops.

The next A boards at the front and nothing is missing again. X leaves: X was never needed, so missing stays 0 and the carriage keeps shrinking. "BCA" is three letters, the best yet. B leaves next and the loop stops.
![X has stepped off too; the carriage holds B, C, A in teal and the readout says s[lo:hi] = "BCA"](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/alg-011-minimum-window/best.jpg)
The last B boards: "CAB" is complete but also three letters, not shorter, so s[lo:hi] stays "BCA". C leaves and the text has ended.
What does the program look like?
This is the program from the video, line for line:
from collections import Counter
def min_window(s, t):
need, missing = Counter(t), len(t)
left, lo, hi = 0, 0, 0
for right, ch in enumerate(s):
if need[ch] > 0: missing -= 1
need[ch] -= 1
while missing == 0:
if not hi or right + 1 - left < hi - lo:
lo, hi = left, right + 1
need[s[left]] += 1
if need[s[left]] > 0: missing += 1
left += 1
return s[lo:hi]
print(min_window("AXBCAB", "ABC"))
Output:
BCA
We also checked it against a brute force that tries every substring, on 3,000 random pairs of strings: the lengths always agree. LeetCode's own first example, min_window("ADOBECODEBANC", "ABC"), returns "BANC".
Why is it O(n)?
The while loop inside the for loop looks quadratic, but count what happens to each letter. It boards exactly once, when the right edge reaches it, and leaves at most once, when the left edge passes it, and both edges only move forward. That is at most 2n steps, each a constant-time Counter update and a comparison of two lengths, plus building the Counter from t: O(n + m) time in all. Keeping lo and hi instead of slicing the string at every step matters: slicing a long window on every shrink step would copy up to n letters each time.

Try it
Keep s = "AXBCAB" and change t to "ABB", so the carriage needs one A and two B's. How long is the shortest window, and which letters are in it?