forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
36 lines (29 loc) · 710 Bytes
/
index.ts
File metadata and controls
36 lines (29 loc) · 710 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
// HELP:
export function findAnagrams(s: string, p: string) {
let len = p.length
let hash: any = {}
let ans: any = {}
let res = []
for (let i = 0, l = p.length; i < l; i++) {
let index = p.charCodeAt(i) - 97
ans[index] = ~~ans[index] + 1
}
for (let i = 0, l = s.length; i < l; i++) {
let index = s.charCodeAt(i) - 97
hash[index] = ~~hash[index] + 1
if (i >= len) {
let index = s.charCodeAt(i - len) - 97
hash[index] = ~~hash[index] - 1
}
if (i + 1 >= len) {
help() && res.push(i - len + 1)
}
}
function help() {
for (let i = 0; i < 26; i++) {
if (~~hash[i] !== ~~ans[i]) return false
}
return true
}
return res
}