-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0567-permutation-in-string.ts
42 lines (35 loc) · 1.15 KB
/
0567-permutation-in-string.ts
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
function checkInclusion(s1: string, s2: string): boolean {
if (s1.length > s2.length) return false;
const s1Count = Array(26).fill(0);
const s2Count = Array(26).fill(0);
for (let i = 0; i < s1.length; i++) {
s1Count[s1[i].charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
s2Count[s2[i].charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
}
let matches = 0;
for (let i = 0; i < 26; i++) {
if (s1Count[i] == s2Count[i]) {
matches += 1;
}
}
let l = 0;
for (let r = s1.length; r < s2.length; r++) {
if (matches == 26) return true;
let index = s2[r].charCodeAt(0) - 'a'.charCodeAt(0);
s2Count[index] += 1;
if (s1Count[index] == s2Count[index]) {
matches += 1;
} else if (s1Count[index] + 1 == s2Count[index]) {
matches -= 1;
}
let index2 = s2[l].charCodeAt(0) - 'a'.charCodeAt(0);
s2Count[index2] -= 1;
if (s1Count[index2] == s2Count[index2]) {
matches += 1;
} else if (s1Count[index2] - 1 == s2Count[index2]) {
matches -= 1;
}
l += 1;
}
return matches == 26;
}