/** * @param {string} s * @param {string} t * @return {string} */ var minWindow = function (s, t) { const need = {}; for (const c of t) need[c] = (need[c] || 0) + 1;
let left = 0; let minLen = Infinity; let minStart = 0; let count = t.length; // 还需要匹配的字符数
for (let right = 0; right < s.length; right++) { if (need[s[right]] !== undefined) { if (need[s[right]] > 0) count--; need[s[right]]--; }
while (count === 0) { if (right - left + 1 < minLen) { minLen = right - left + 1; minStart = left; } if (need[s[left]] !== undefined) { need[s[left]]++; if (need[s[left]] > 0) count++; } left++; } }