-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
39 lines (39 loc) · 746 Bytes
/
Copy pathmain.js
File metadata and controls
39 lines (39 loc) · 746 Bytes
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
/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function (s, p) {
let cnt = 0
const dcts = {}
const map = {}
const ans = []
for (const c of p) {
if (dcts[c] == null) dcts[c] = 0
dcts[c] ++
map[c] = 0
}
for (let i = 0; i < p.length - 1; i++) {
const c = s[i]
if (c in dcts && map[c] < dcts[c]) {
map[c]++
cnt++
}
}
for (let i = p.length - 1; i < s.length; i++) {
let c = s[i]
if (c in dcts) {
if (map[c] < dcts[c]) cnt++
map[c]++
}
if (cnt === p.length) {
ans.push(i - p.length + 1)
}
c = s[i - (p.length - 1)]
if (c in dcts) {
if (map[c] <= dcts[c]) cnt--
map[c] --
}
}
return ans
}