Minimum window substring
A variable-size sliding window with a character-count map: grow the right edge until every needed letter is covered, then shrink the left edge as far as it will go.
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.The idea
need counts every character required by t. window counts what the current [left, right] slice holds. formed counts how many distinct characters already have enough copies. Once formed equals the number of distinct characters needed, the window is valid, so shrink it from the left to look for something smaller before growing again.
Code
public static string MinWindow(string s, string t) {
var need = new Dictionary<char, int>();
foreach (char c in t) need[c] = need.GetValueOrDefault(c) + 1;
int required = need.Count, formed = 0, left = 0;
var window = new Dictionary<char, int>();
int bestLen = int.MaxValue, bestStart = 0;
for (int right = 0; right < s.Length; right++) {
char c = s[right];
window[c] = window.GetValueOrDefault(c) + 1;
if (need.ContainsKey(c) && window[c] == need[c]) formed++;
while (formed == required) {
if (right - left + 1 < bestLen) { bestLen = right - left + 1; bestStart = left; }
char lc = s[left];
window[lc]--;
if (need.ContainsKey(lc) && window[lc] < need[lc]) formed--;
left++;
}
}
return bestLen == int.MaxValue ? "" : s.Substring(bestStart, bestLen);
}static String minWindow(String s, String t) {
Map<Character, Integer> need = new HashMap<>();
for (char c : t.toCharArray()) need.merge(c, 1, Integer::sum);
int required = need.size(), formed = 0, left = 0;
Map<Character, Integer> window = new HashMap<>();
int bestLen = Integer.MAX_VALUE, bestStart = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
window.merge(c, 1, Integer::sum);
if (need.containsKey(c) && window.get(c).intValue() == need.get(c).intValue()) formed++;
while (formed == required) {
if (right - left + 1 < bestLen) { bestLen = right - left + 1; bestStart = left; }
char lc = s.charAt(left);
window.merge(lc, -1, Integer::sum);
if (need.containsKey(lc) && window.get(lc) < need.get(lc)) formed--;
left++;
}
}
return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestStart, bestStart + bestLen);
}string minWindow(const string& s, const string& t) {
unordered_map<char, int> need;
for (char c : t) need[c]++;
int required = (int)need.size(), formed = 0, left = 0;
unordered_map<char, int> window;
int bestLen = INT_MAX, bestStart = 0;
for (int right = 0; right < (int)s.size(); right++) {
char c = s[right];
window[c]++;
if (need.count(c) && window[c] == need[c]) formed++;
while (formed == required) {
if (right - left + 1 < bestLen) { bestLen = right - left + 1; bestStart = left; }
char lc = s[left];
window[lc]--;
if (need.count(lc) && window[lc] < need[lc]) formed--;
left++;
}
}
return bestLen == INT_MAX ? "" : s.substr(bestStart, bestLen);
}def min_window(s: str, t: str) -> str:
need = {}
for c in t: need[c] = need.get(c, 0) + 1
required, formed, left = len(need), 0, 0
window = {}
best_len, best_start = float("inf"), 0
for right in range(len(s)):
c = s[right]
window[c] = window.get(c, 0) + 1
if c in need and window[c] == need[c]: formed += 1
while formed == required:
if right - left + 1 < best_len: best_len = right - left + 1; best_start = left
lc = s[left]
window[lc] -= 1
if lc in need and window[lc] < need[lc]: formed -= 1
left += 1
return "" if best_len == float("inf") else s[best_start:best_start + best_len]Tips
- A character only increases formed the moment its count exactly reaches what is needed, not every time it appears.
- Shrinking only happens while the window stays valid; the moment removing s[left] would break it, left stops and right grows again.
In the .NET library
This is the hardest sliding-window shape: both edges move independently, driven by the formed == required condition rather than a fixed size or a simple threshold.