-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76. Minimum Window Substring.js
43 lines (33 loc) · 1 KB
/
76. Minimum Window Substring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var minWindow = function (s, t) {
if (!s || !t) {
return "";
}
let dictT = new Map();
for (let c of t)
dictT.set(c, (dictT.get(c) || 0) + 1);
let required = dictT.size;
let l = 0, r = 0;
let formed = 0;
let windowCounts = new Map();
let ans = [-1, 0, 0];
while (r < s.length) {
let c = s.charAt(r);
windowCounts.set(c, (windowCounts.get(c) || 0) + 1);
if (dictT.has(c) && windowCounts.get(c) === dictT.get(c))
formed++;
while (l <= r && formed === required) {
c = s.charAt(l);
if (ans[0] === -1 || r - l + 1 < ans[0]) {
ans[0] = r - l + 1;
ans[1] = l;
ans[2] = r;
}
windowCounts.set(c, windowCounts.get(c) - 1);
if (dictT.has(c) && windowCounts.get(c) < dictT.get(c))
formed--;
l++;
}
r++;
}
return ans[0] === -1 ? "" : s.substring(ans[1], ans[2] + 1);
};