Skip to content

Sliding Window — Minimum Window Substring ​

↗ LeetCode: Minimum Window Substring · Hard · pattern chapter →

Asked atMetaAmazonGoogleMicrosoftLinkedInUber

Given strings s and t, return the smallest substring of s that contains every character of t (with multiplicity). Return "" if impossible.

Example 1 — s = "ADOBECODEBANC", t = "ABC" → "BANC"Example 2 — s = "a", t = "a" → "a"Example 3 — s = "a", t = "aa" → "" (not enough as in s)

Constraints — 1 ≤ m, n ≤ 10⁵. Uppercase + lowercase ASCII.

Progressive Hints
Try to solve it yourself first. Reveal hints only when stuck — each hint is progressively more specific.
---
⏱️Interview timer00:00
Practice under interview pressure. Start when you begin reading; stop when you finish. Save best only when you had a full working solution.
Target for interview: Medium = 20-25 min · Hard = 30-40 min · Best time persists on this device only.

Approach 1 — Enumerate every substring ​

Intuition. Try every s[i..j]; for each, check whether it contains all characters of t. Keep the shortest.

java
String minWindowBrute(String s, String t) {
    int n = s.length(), bestL = -1, bestLen = Integer.MAX_VALUE;
    for (int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
            if (contains(s, i, j, t) && j - i + 1 < bestLen) {
                bestLen = j - i + 1; bestL = i;
            }
    return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestL, bestL + bestLen);
}
boolean contains(String s, int l, int r, String t) {
    int[] need = new int[128];
    for (char c : t.toCharArray()) need[c]++;
    for (int i = l; i <= r; i++) if (need[s.charAt(i)] > 0) need[s.charAt(i)]--;
    for (int x : need) if (x > 0) return false;
    return true;
}
Execution Trace
Brute — s='ADOBEC', t='ABC' (first valid window)
ijA0D1O2B3E4C5substr = Acontains = no
Step 1 / 4missing B, C

Complexity — Time O(n² · (n + σ)); Space O(σ).

At n = 10⁵ this is ~10¹⁵ ops. TLE.


Approach 2 — Sliding window with need/have counter ​

Insight from brute. For a fixed left, the smallest valid right grows monotonically as left advances. So we never restart right — extend right until the window is valid, then shrink left while still valid, tracking the best.

Track need[c] (positive for chars we still need) and have = number of characters currently satisfied. Window is valid iff have == t.length().

Trap — decrement need[c] for every char in s (may go negative for over-represented chars). Only increment have when the decrement crossed from >0 to ≥0 — i.e., need[c]-- > 0 before decrement.

java
String minWindow(String s, String t) {
    int[] need = new int[128];
    for (char c : t.toCharArray()) need[c]++;
    int required = t.length(), have = 0;
    int bestL = 0, bestLen = Integer.MAX_VALUE, left = 0;
    for (int right = 0; right < s.length(); right++) {
        char c = s.charAt(right);
        if (need[c]-- > 0) have++;
        while (have == required) {
            if (right - left + 1 < bestLen) { bestLen = right - left + 1; bestL = left; }
            char lc = s.charAt(left++);
            if (++need[lc] > 0) have--;
        }
    }
    return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestL, bestL + bestLen);
}
Execution Trace
Sliding window — s='ADOBECODEBANC', t='ABC'
leftrightA0D1O2B3E4C5O6D7E8B9A10N11C12have = 3window = ADOBECbest = 6
Step 1 / 4first valid window at r=5; try to shrink

Complexity — Time O(n + m) — each char enters and leaves the window at most once; Space O(σ).


Try it yourself ​

Complexity summary ​

ApproachTimeSpaceInterview grade
Enumerate every substringO(n² · (n+σ))O(σ)baseline; TLE at n=10⁵
Sliding window + need/haveO(n + m)O(σ)expected optimum

When to use which ​

  • First pass — always state brute for signal, then jump to sliding window.
  • "Contains every char of t" → need/have counter; skeleton reusable.
  • "What if t may repeat characters?" → the need[] array handles it because you compare counts, not presence.
  • Follow-up: return all min windows → track ties, or convert to two-pointer with restart.
✨AI Study CompanionBeta
Was this page helpful?

Released under MIT license.

💡 Quick shortcuts
H HintsR Run JavaA AI Help