Minimum Window Substring
Hard · Sliding window
Jump to codeFind the shortest substring of s containing every character of t, including repeated copies. Return an empty string if no such window exists; if t is empty, also return an empty string.
Examples
Example 1
s = "ADOBECODEBANC" t = "ABC"
Output: "BANC"
Example 2
s = "a" t = "a"
Output: "a"
Constraints
- 1 <= s.length <= 1000
- 1 <= t.length <= 1000
- s and t consist of uppercase and lowercase English letters.
Need a hint?
Track how many required character copies are still missing.
Asked in real interviews
Candidates reported versions of Minimum Window Substring from these company interviews. Some company questions are locked and open with Premium or XP.
- Minimum Window Substring with Repeated Required CharactersLyft
- Solve min window & animal conflictsLinkedIn
- Find the Minimum Window Containing a Multiset of CharactersByteDance
Solution approach
Count requirements from t. Expand right, reducing the missing count only when a required copy is filled. Once all copies are covered, shrink left while recording the shortest window; stop shrinking when removal creates a deficit. Each pointer moves only forward.
Complexity
O(|s| + |t|) expected time and O(alphabet size) space.
Python reference solution
from collections import Counter
def minWindow(s, t):
if not t:
return ''
need = Counter(t)
missing, left = len(t), 0
best_start, best_length = 0, len(s) + 1
for right, char in enumerate(s):
if need[char] > 0:
missing -= 1
need[char] -= 1
while missing == 0:
if right - left + 1 < best_length:
best_start, best_length = left, right - left + 1
outgoing = s[left]
need[outgoing] += 1
if need[outgoing] > 0:
missing += 1
left += 1
return '' if best_length > len(s) else s[best_start:best_start + best_length]
JavaScript solution
function minWindow(s, t) {
if (!t.length) return '';
const need = new Map();
for (const c of t) need.set(c, (need.get(c) || 0) + 1);
let missing = t.length, left = 0, start = 0, length = s.length + 1;
for (let right = 0; right < s.length; right++) {
const c = s[right];
if ((need.get(c) || 0) > 0) missing--;
need.set(c, (need.get(c) || 0) - 1);
while (missing === 0) {
if (right - left + 1 < length) {
start = left; length = right - left + 1;
}
const outgoing = s[left++];
need.set(outgoing, need.get(outgoing) + 1);
if (need.get(outgoing) > 0) missing++;
}
}
return length > s.length ? '' : s.slice(start, start + length);
}
Java solution
import java.util.*;
class Solution {
public String minWindow(String s, String t) {
if (t.isEmpty()) return "";
Map<Character, Integer> need = new HashMap<>();
for (int i = 0; i < t.length(); i++) need.merge(t.charAt(i), 1, Integer::sum);
int missing = t.length(), left = 0, start = 0, length = s.length() + 1;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (need.getOrDefault(c, 0) > 0) missing--;
need.put(c, need.getOrDefault(c, 0) - 1);
while (missing == 0) {
if (right - left + 1 < length) {
start = left; length = right - left + 1;
}
char outgoing = s.charAt(left++);
need.put(outgoing, need.get(outgoing) + 1);
if (need.get(outgoing) > 0) missing++;
}
}
return length > s.length() ? "" : s.substring(start, start + length);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string minWindow(string s, string t) {
if (t.empty()) return "";
array<int, 256> need{};
for (unsigned char c : t) need[c]++;
int missing = t.size(), left = 0, start = 0, length = s.size() + 1;
for (int right = 0; right < (int)s.size(); right++) {
if (need[(unsigned char)s[right]]-- > 0) missing--;
while (missing == 0) {
if (right - left + 1 < length) {
start = left; length = right - left + 1;
}
if (++need[(unsigned char)s[left++]] > 0) missing++;
}
}
return length > (int)s.size() ? "" : s.substr(start, length);
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 7 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[ "ADOBECODEBANC", "ABC" ]
BANC
[ "a", "a" ]
a
[ "a", "aa" ]
[ "aa", "aa" ]
aa
[ "ab", "b" ]
b
Drafts are saved per language in this browser when storage is available.