01
Problem Statement
Given two strings s and t, return the minimum window substring of s that contains every character in t (including duplicates). If there is no such window, return the empty string "".
- The window must contain at least the same count of each character as
t(e.g. ift = "AAB", the window must have at least 2 A's and 1 B). - If
tis longer thans, return"". If multiple minimum windows exist, return any one of them. - Characters in
sthat are not intcan appear in the window; we only care about satisfying the required counts fort.
02
Key Observations
- We need a variable-size sliding window that "covers"
t: the window has, for each character int, at least as many occurrences as int. We can build a frequency map fortand maintain the window's counts; "required" can be tracked as the number of distinct characters that are still under their target count (or we check if all targets are met). - Expand with
rightuntil the window coverst; then shrink fromleftwhile the window still coverst, to get the smallest valid window ending atright. Track the minimum length and the corresponding substring. - When we add a character that is in
t, we update the window count; when that count reaches the target for that character, we can decrement a "required" counter. When required becomes 0, the window coverst.
03
Approach
High-level: Maintain a window [left, right] and frequency counts for t and for the current window. Expand right until the window contains all characters of t (with correct counts); then shrink left while the window still covers t, and record the minimum window. Return the substring for that minimum window.
Steps:
- Build
freqfort(how many of each char we need). Maintainwindowcounts and a "required" count (e.g. number of distinct chars that are still short of target). - For each
right: adds[right]towindow. Ifs[right]is intandwindownow meets the target for that char, decrement required. Whenrequired == 0, the window coverst. - While the window still covers
t, update the minimum length and substring, then removes[left]andleft++. - Return the minimum substring (or
""if never found).
04
Implementation
Java
public String minWindow(String s, String t) {
if (t.length() > s.length()) {
return "";
}
Map<Character, Integer> need = new HashMap<>();
for (char c : t.toCharArray()) {
need.merge(c, 1, Integer::sum);
}
int required = need.size(), left = 0, minStart = 0, minLen = Integer.MAX_VALUE;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (need.containsKey(c)) {
need.merge(c, -1, Integer::sum);
if (need.get(c) == 0) {
required--;
}
}
while (required == 0) {
if (right - left + 1 < minLen) {
minLen = right - left + 1;
minStart = left;
}
char l = s.charAt(left);
if (need.containsKey(l)) {
if (need.get(l) == 0) {
required++;
}
need.merge(l, 1, Integer::sum);
}
left++;
}
}
return minLen == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLen);
}05
Test Cases
Example 1
Input
s = "ADOBECODEBANC", t = "ABC"
Expected
"BANC"Explanation
Minimum window containing A, B, C.
06
Complexity Analysis
- Time: O(|s| + |t|).
- Space: O(1) or O(|t|).
07
Follow-ups
- Return all minimum windows; handle duplicate chars in t correctly.