76. Minimum Window Substring
202407141234
tags: #sliding-window
var minWindow = function(s, t) {
const map = new Map();
for (const c of t) {
map.set(c, (map.get(c) ?? 0) + 1);
}
let start = 0;
let counter = 0;
let minLen = Number.POSITIVE_INFINITY;
let minStart = -1;
for (let end = 0; end < s.length; end++) {
map.set(s[end], (map.get(s[end]) ?? 0) - 1);
if (map.get(s[end]) >= 0) {
counter += 1;
}
while (counter === t.length) {
if (end - start + 1 < minLen) {
minLen = end - start + 1;
minStart = start;
}
map.set(s[start], map.get(s[start]) + 1);
if (map.get(s[start]) > 0) {
counter -= 1;
}
start += 1;
}
}
return minLen === Number.POSITIVE_INFINITY ? "" : s.substring(minStart, minStart + minLen);
};