DSA sliding window learning path Level 3 This level · 2 of 3
Hard dsa sliding-window

Minimum Window Substring

The 30 second answer

I need the shortest substring of s that covers every character of t, including duplicates. I grow a window until it satisfies the required counts, then shrink from the left as far as it still does, recording the shortest window I see. If nothing covers t I return an empty string.

Could you say this in an interview?

Problem Given strings s and t, return the shortest substring of s that contains every character of t, including multiplicity. If several substrings have that length, return the first one that appears. If there is no such substring, return the empty string. Original: LeetCode 76: Minimum Window Substring.

Understanding the prompt Characters not in t can sit inside the window; they just make it longer, so you will try to shrink them away. Extra copies of a needed character beyond t's count are also shrinkable. Letter case matters, and t may use letters outside a to z, so a 128-slot count is safer than 26.

Pattern recognition A variable-size window that tracks how many distinct required characters are currently satisfied, then shrinks to the shortest covering window for each right edge.

Brute force Test every substring of s against the counts of t: O(n² · alphabet).

Optimal Count the letters of t and how many distinct letters still have a shortfall (missing). Grow hi, and when a letter's window count first reaches its need, decrement missing. While missing is 0 the window is valid: record it if it is the shortest so far, then drop s[lo] and, if that letter falls below its need, increment missing again.

Alternatives A HashMap of only the characters that appear in t is the same algorithm and uses less space on a large alphabet. Two pointers plus a missing-count is the version to write on a whiteboard.

Java solution

public class MinimumWindowSubstring {
    static String minWindow(String s, String t) {
        if (t.isEmpty() || s.length() < t.length()) return "";
        var need = new int[128];
        int missing = 0;
        for (int i = 0; i < t.length(); i++) {
            if (need[t.charAt(i)]++ == 0) missing++;
        }
        var have = new int[128];
        int formed = 0, lo = 0, bestLo = 0, bestLen = Integer.MAX_VALUE;
        for (int hi = 0; hi < s.length(); hi++) {
            char c = s.charAt(hi);
            if (++have[c] == need[c] && need[c] > 0) formed++;
            while (formed == missing) {
                if (hi - lo + 1 < bestLen) {
                    bestLen = hi - lo + 1;
                    bestLo = lo;
                }
                char left = s.charAt(lo++);
                if (need[left] > 0 && --have[left] < need[left]) formed--;
            }
        }
        return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestLo, bestLo + bestLen);
    }

    public static void main(String[] args) {
        System.out.println(minWindow("ADOBECODEBANC", "ABC")); // BANC
        System.out.println(minWindow("a", "a"));               // a
        System.out.println(minWindow("a", "aa"));              // (empty)
        System.out.println(minWindow("a", "b"));               // (empty)
    }
}

Complexity O(|s| + |t|) time, O(1) extra space if you treat the alphabet as fixed (here 128 ASCII slots), otherwise O(alphabet).

Edge cases

  • t longer than s, or a letter in t that never appears, returns "".
  • s and t are equal, as in ("a", "a"), returns "a".
  • The official sample s = "ADOBECODEBANC", t = "ABC" returns "BANC".

Follow-ups

  • Permutation in String is the fixed-length special case: the window size equals t.
  • Smallest window that covers a set of words rather than characters is a similar shrink, with a different counter.

Tests ("ADOBECODEBANC", "ABC") returns "BANC". ("a", "a") returns "a". ("a", "aa") and ("a", "b") return "".

How to say it in the interview "I grow a window until it covers every character of t, counting how many required letters are still short. Then I shrink from the left as far as the window stays valid, and I remember the shortest one. If I never cover t I return an empty string."

Sources (2)
  1. LeetCode 76: Minimum Window Substringleetcode.com
  2. Java SE 17 API: String.substringdocs.oracle.com
Previous
Next