Skip to content

Commit c3f18e1

Browse files
committed
feat: add question 567
1 parent 7ce69bc commit c3f18e1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

567.data

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"ab"
2+
"eidbaooo"
3+
"ab"
4+
"eidboaoo"

567.字符串的排列.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @lc app=leetcode.cn id=567 lang=javascript
3+
*
4+
* [567] 字符串的排列
5+
*
6+
* 1. 计算 s1 中每个字母出现次数
7+
* 2. 以 s1 的长度作为窗口长度, 每次计算 s2 在窗口长度内各字母出现次数
8+
* 3. 对比次数, 一致则返回结果
9+
* 4. 每次更新窗口后, 只需要进入创建字母次数++, 和移出窗口字母次数--, 即可
10+
*/
11+
12+
// @lc code=start
13+
/**
14+
* @param {string} s1
15+
* @param {string} s2
16+
* @return {boolean}
17+
*/
18+
var checkInclusion = function(s1, s2) {
19+
var list1 = new Array(26).fill(0);
20+
for (var i = 0; i < s1.length; i++) {
21+
list1[s1.charCodeAt(i) - 97]++;
22+
}
23+
24+
var list2 = new Array(26).fill(0);
25+
for (var i = 0; i < s1.length; i++) {
26+
list2[s2.charCodeAt(i) - 97]++;
27+
}
28+
29+
function match() {
30+
for (var i = 0; i < 26; i++) {
31+
if (list1[i] !== list2[i]) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
if (match()) {
39+
return true;
40+
}
41+
42+
for (var i = s1.length; i < s2.length; i++) {
43+
list2[s2.charCodeAt(i) - 97]++;
44+
list2[s2.charCodeAt(i - s1.length) - 97]--;
45+
46+
if (match()) {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
};
53+
// @lc code=end
54+

0 commit comments

Comments
 (0)