#76

Minimum Window Substring

Hard Variable window with need counts

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the shortest substring of s that contains every character of t (with multiplicity). Return "" if none exists.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the shortest part of s containing all letters of t.

Why this works: expand until everything is covered, then shrink from the left while it stays covered. A single "missing" counter tells you when you're covered.

Example: s = "ADOBECODEBANC", t = "ABC"

first cover "ADOBEC" (6)keep sliding … "BANC" covers A, B, C (4)answer "BANC"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Comparing full count maps on every step — correct but much slower and error-prone.

Solve on LeetCode ↗
Complexity
  • Time: O(|s| + |t|).
  • Space: O(128).
Topic: Sliding Window
🎬 Episode 10: video coming soon