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.
Examples
Example 1
`s = "ADOBECODEBANC", t = "ABC"``"BANC"`Example 2
`s = "a", t = "a"``"a"`Example 3
`s = "a", t = "aa"``""`Example 4
`s = "a", t = "b"``""`Constraints
m == s.lengthn == t.length1 <= m, n <= 10^5sandtconsist of uppercase and lowercase English letters.
Hints
Hint 1
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.
Hint 2
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.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
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.
Alternative. 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.
Complexity
- Time
- O(|s| + |t|)
O(|s| + |t|) time, O(1) extra space if you treat the alphabet as fixed (here 128 ASCII slots), otherwise O(alphabet).
- Space
- O(alphabet)
Code
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)
}
}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.
Interview script
"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."
Source: LeetCode 76: Minimum Window Substring Source: Java SE 17 API: String.substring)
Sources (2)
- LeetCode 76: Minimum Window Substringleetcode.com
- Java SE 17 API: String.substringdocs.oracle.com