-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path438.找到字符串中所有字母异位词.js
52 lines (50 loc) · 1.08 KB
/
438.找到字符串中所有字母异位词.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
44
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode.cn id=438 lang=javascript
*
* [438] 找到字符串中所有字母异位词
*/
// @lc code=start
/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function(s, p) {
const sLen = s.length,
pLen = p.length;
const ret = [];
if (sLen < pLen) {
return ret;
}
const count = Array.from({ length: 26 }, () => 0),
tempCount = Array.from({ length: 26 }, () => 0);
for (let i = 0; i < p.length; i++) {
count[p.charCodeAt(i) - 97]++;
tempCount[s.charCodeAt(i) - 97]++;
}
if (isEqualArray(count, tempCount)) {
ret.push(0);
}
let start = 1;
while (start <= sLen - pLen) {
tempCount[s.charCodeAt(start - 1) - 97]--;
tempCount[s.charCodeAt(start + pLen - 1) - 97]++;
if (isEqualArray(count, tempCount)) {
ret.push(start);
}
start++;
}
return ret;
};
function isEqualArray(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
// @lc code=end