-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
43 lines (42 loc) · 1.12 KB
/
main.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
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
var minWindow = function(s, t) {
//初始化双指针,维护滑动窗口
let l = 0;
let r = 0;
//字典: 滑动窗口需要的字符 和 它的个数
const need = new Map();
for(let c of t){
need.set(c,need.has(c)?need.get(c)+1:1)
}
//eg 对于"ABC"的输入,need是 Map{'A'=>1,'B'=>1,'C'=>1}
let needType = need.size;
let res = '';
//移动右指针
while(r<s.length){
//获取当前字符
const c = s[r];
//如果字符在需要的字符列表里
if(need.has(c)){
need.set(c,need.get(c)-1);
if(need.get(c) === 0) needType -=1;
}
//如果找到包含t的子串,就移动左指针
while(needType === 0){
//找到包含t的子串其中最小的
const newRes = s.substring(l,r+1);
if(!res || newRes.length < res.length) res = newRes;
const c2 = s[l];
if(need.has(c2)){
need.set(c2,need.get(c2)+1);
if(need.get(c2) === 1) needType+=1;
}
l+=1;
}
r+=1;
}
return res;
};