-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path438.找到字符串中所有字母异位词.js
More file actions
66 lines (64 loc) · 1.63 KB
/
438.找到字符串中所有字母异位词.js
File metadata and controls
66 lines (64 loc) · 1.63 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
//
// 异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
//
//
//
// 示例 1:
//
//
//输入: s = "cbaebabacd", p = "abc"
//输出: [0,6]
//解释:
//起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
//起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
//
//
// 示例 2:
//
//
//输入: s = "abab", p = "ab"
//输出: [0,1,2]
//解释:
//起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
//起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
//起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
//
//
//
//
// 提示:
//
//
// 1 <= s.length, p.length <= 3 * 10⁴
// s 和 p 仅包含小写字母
//
//
// Related Topics 哈希表 字符串 滑动窗口 👍 993 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function (s, p) {
const len = p.length
const result = []
for (let i = 0; i < s.length - len + 1; i++) {
const template = new Array(26).fill(0)
for (let i = 0; i < len; i++) {
template[p[i].charCodeAt() - 97]++
}
for(let j = 0; j < len; j++) {
template[s[i + j].charCodeAt() - 97]--
if(template[s[i + j].charCodeAt() - 97] < 0) {
continue
}
}
if(template.every(item => item === 0)) {
result.push(i)
}
}
return result
};
//leetcode submit region end(Prohibit modification and deletion)