string minWindow(string s, string t) {
auto covers = [&](const string& w) {
int cnt[256] = {0};
for (unsigned char c : t) cnt[c]++;
for (unsigned char c : w) cnt[c]--;
for (unsigned char c : t) if (cnt[c] > 0) return false;
return true;
};
int n = s.size();
for (int len = t.size(); len <= n; len++) {
for (int i = 0; i + len <= n; i++) {
string window = s.substr(i, len);
if (covers(window)) return window;
}
}
return "";
}
public String minWindow(String s, String t) {
int n = s.length();
for (int len = t.length(); len <= n; len++) {
for (int i = 0; i + len <= n; i++) {
String window = s.substring(i, i + len);
if (covers(window, t)) return window;
}
}
return "";
}
private boolean covers(String w, String t) {
int[] cnt = new int[128];
for (char c : t.toCharArray()) cnt[c]++;
for (char c : w.toCharArray()) cnt[c]--;
for (char c : t.toCharArray()) if (cnt[c] > 0) return false;
return true;
}
def min_window(s: str, t: str) -> str:
def covers(w: str) -> bool:
return all(w.count(ch) >= t.count(ch) for ch in set(t))
for length in range(len(t), len(s) + 1):
for i in range(len(s) - length + 1):
window = s[i:i + length]
if covers(window): return window
return ""
function minWindow(s: string, t: string): string {
const covers = (w: string) => [...t].every((ch) => w.split(ch).length >= t.split(ch).length);
for (let len = t.length; len <= s.length; len++) {
for (let i = 0; i + len <= s.length; i++) {
const window = s.slice(i, i + len);
if (covers(window)) return window;
}
}
return '';
}
string minWindow(string s, string t) {
unordered_map<char, int> need;
for (char ch : t) need[ch]++;
unordered_map<char, int> have;
auto satisfied = [&]() { for (auto& [ch, n] : need) if (have[ch] < n) return false; return true; };
int bestStart = 0;
int bestLen = INT_MAX;
int left = 0;
for (int right = 0; right < (int)s.size(); right++) {
have[s[right]]++;
while (satisfied()) {
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
have[s[left]]--;
left++;
}
}
return bestLen == INT_MAX ? "" : s.substr(bestStart, bestLen);
}
public String minWindow(String s, String t) {
Map<Character, Integer> need = new HashMap<>();
for (char ch : t.toCharArray()) need.merge(ch, 1, Integer::sum);
Map<Character, Integer> have = new HashMap<>();
int bestStart = 0;
int bestLen = Integer.MAX_VALUE;
int left = 0;
for (int right = 0; right < s.length(); right++) {
have.merge(s.charAt(right), 1, Integer::sum);
while (satisfied(need, have)) {
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
have.merge(s.charAt(left), -1, Integer::sum);
left++;
}
}
return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestStart, bestStart + bestLen);
}
private boolean satisfied(Map<Character, Integer> need, Map<Character, Integer> have) {
for (Map.Entry<Character, Integer> e : need.entrySet())
if (have.getOrDefault(e.getKey(), 0) < e.getValue()) return false;
return true;
}
def min_window(s: str, t: str) -> str:
need: dict[str, int] = {}
for ch in t: need[ch] = need.get(ch, 0) + 1
have: dict[str, int] = {}
def satisfied() -> bool: return all(have.get(ch, 0) >= n for ch, n in need.items())
best_start = 0
best_len = float('inf')
left = 0
for right in range(len(s)):
have[s[right]] = have.get(s[right], 0) + 1
while satisfied():
if right - left + 1 < best_len:
best_len = right - left + 1
best_start = left
have[s[left]] -= 1
left += 1
return '' if best_len == float('inf') else s[best_start:best_start + best_len]
function minWindow(s: string, t: string): string {
const need = new Map<string, number>();
for (const ch of t) need.set(ch, (need.get(ch) ?? 0) + 1);
const have = new Map<string, number>();
const satisfied = () => [...need].every(([ch, n]) => (have.get(ch) ?? 0) >= n);
let bestStart = 0;
let bestLen = Infinity;
let left = 0;
for (let right = 0; right < s.length; right++) {
have.set(s[right], (have.get(s[right]) ?? 0) + 1);
while (satisfied()) {
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
have.set(s[left], have.get(s[left])! - 1);
left++;
}
}
return bestLen === Infinity ? '' : s.slice(bestStart, bestStart + bestLen);
}
string minWindow(string s, string t) {
if (t.size() > s.size()) return "";
unordered_map<char, int> need;
for (char ch : t) need[ch]++;
int missing = t.size();
int bestStart = 0;
int bestLen = INT_MAX;
int left = 0;
for (int right = 0; right < (int)s.size(); right++) {
char c = s[right];
if (need[c] > 0) missing--;
need[c]--;
while (missing == 0) {
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
char d = s[left];
need[d]++;
if (need[d] > 0) missing++;
left++;
}
}
return bestLen == INT_MAX ? "" : s.substr(bestStart, bestLen);
}
public String minWindow(String s, String t) {
if (t.length() > s.length()) return "";
Map<Character, Integer> need = new HashMap<>();
for (char ch : t.toCharArray()) need.merge(ch, 1, Integer::sum);
int missing = t.length();
int bestStart = 0;
int bestLen = Integer.MAX_VALUE;
int left = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (need.getOrDefault(c, 0) > 0) missing--;
need.merge(c, -1, Integer::sum);
while (missing == 0) {
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
char d = s.charAt(left);
need.merge(d, 1, Integer::sum);
if (need.get(d) > 0) missing++;
left++;
}
}
return bestLen == Integer.MAX_VALUE ? "" : s.substring(bestStart, bestStart + bestLen);
}
def min_window(s: str, t: str) -> str:
if len(t) > len(s): return ''
need: dict[str, int] = {}
for ch in t: need[ch] = need.get(ch, 0) + 1
missing = len(t)
best_start = 0
best_len = float('inf')
left = 0
for right in range(len(s)):
c = s[right]
if need.get(c, 0) > 0: missing -= 1
need[c] = need.get(c, 0) - 1
while missing == 0:
if right - left + 1 < best_len:
best_len = right - left + 1
best_start = left
d = s[left]
need[d] += 1
if need[d] > 0: missing += 1
left += 1
return '' if best_len == float('inf') else s[best_start:best_start + best_len]
function minWindow(s: string, t: string): string {
if (t.length > s.length) return '';
const need = new Map<string, number>();
for (const ch of t) need.set(ch, (need.get(ch) ?? 0) + 1);
let missing = t.length;
let bestStart = 0;
let bestLen = Infinity;
let left = 0;
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 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
}
const d = s[left];
need.set(d, need.get(d)! + 1);
if (need.get(d)! > 0) missing++;
left++;
}
}
return bestLen === Infinity ? '' : s.slice(bestStart, bestStart + bestLen);
}
string minWindow(const string& s, const string& t) {
int n = s.size();
int m = t.size();
if (m == 0 || m > n) return "";
int need[256] = {0}; // one slot per byte value
for (int i = 0; i < m; i++) need[(unsigned char)t[i]]++;
int missing = m;
int bestStart = 0;
int bestLen = n + 1;
int left = 0;
for (int right = 0; right < n; right++) {
if (need[(unsigned char)s[right]]-- > 0) missing--;
if (missing > 0) continue;
while (need[(unsigned char)s[left]] < 0) need[(unsigned char)s[left++]]++;
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
if (bestLen == m) break;
}
need[(unsigned char)s[left++]]++;
missing++;
}
return bestLen > n ? "" : s.substr(bestStart, bestLen);
}
public String minWindow(String s, String t) {
int n = s.length();
int m = t.length();
if (m == 0 || m > n) return "";
int[] need = new int[128]; // ASCII only: other characters throw
for (int i = 0; i < m; i++) need[t.charAt(i)]++;
int missing = m;
int bestStart = 0;
int bestLen = n + 1;
int left = 0;
for (int right = 0; right < n; right++) {
if (need[s.charAt(right)]-- > 0) missing--;
if (missing > 0) continue;
while (need[s.charAt(left)] < 0) need[s.charAt(left++)]++;
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
if (bestLen == m) break;
}
need[s.charAt(left++)]++;
missing++;
}
return bestLen > n ? "" : s.substring(bestStart, bestStart + bestLen);
}
def min_window(s: str, t: str) -> str:
n = len(s)
m = len(t)
if m == 0 or m > n: return ''
need = [0] * 128 # ASCII only: other characters raise IndexError
for ch in t: need[ord(ch)] += 1
missing = m
best_start = 0
best_len = n + 1
left = 0
for right in range(n):
c = ord(s[right])
need[c] -= 1
if need[c] >= 0: missing -= 1
if missing > 0: continue
while need[ord(s[left])] < 0:
need[ord(s[left])] += 1
left += 1
if right - left + 1 < best_len:
best_len = right - left + 1
best_start = left
if best_len == m: break
need[ord(s[left])] += 1
left += 1
missing += 1
return '' if best_len > n else s[best_start:best_start + best_len]
function minWindow(s: string, t: string): string {
const n = s.length;
const m = t.length;
if (m === 0 || m > n) return '';
const need = new Int32Array(128);
for (let i = 0; i < m; i++) need[t.charCodeAt(i)]++;
let missing = m;
let bestStart = 0;
let bestLen = n + 1;
let left = 0;
for (let right = 0; right < n; right++) {
if (need[s.charCodeAt(right)]-- > 0) missing--;
if (missing > 0) continue;
while (need[s.charCodeAt(left)] < 0) need[s.charCodeAt(left++)]++;
if (right - left + 1 < bestLen) {
bestLen = right - left + 1;
bestStart = left;
if (bestLen === m) break;
}
need[s.charCodeAt(left++)]++;
missing++;
}
return bestLen > n ? '' : s.slice(bestStart, bestStart + bestLen);
}