-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path438-FindAllAnagramsInAString.js
40 lines (40 loc) · 1.06 KB
/
438-FindAllAnagramsInAString.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
/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
// 思路:slide Window
var findAnagrams = function(s, p) {
let result = [];
let map = Object.create(null)
for(let i = 0;i<p.length; i++){
let tempChar = p.charAt(i)
map[tempChar] ? map[tempChar]++ : (map[tempChar] = 1)
}
let temp = JSON.parse(JSON.stringify(map))
let windowPointer = 0;
let count = p.length;
for(let i = 0; i<s.length; i++){
let tempChar = s.charAt(i)
if (!map[tempChar]){
temp = JSON.parse(JSON.stringify(map))
windowPointer = i+1
count = p.length
continue
}
temp[tempChar]--;
count --
while (temp[tempChar] < 0){
let c = s.charAt(windowPointer++)
temp[c]++
count++
}
if (count === 0){
result.push(i - p.length + 1)
let c = s.charAt(windowPointer++)
temp[c] = 1
count++
}
}
return result
};