forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
53 lines (47 loc) · 1.48 KB
/
Solution.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
43
44
45
46
47
48
49
50
51
52
53
function countCompleteSubstrings(word: string, k: number): number {
const f = (s: string): number => {
const m = s.length;
let ans = 0;
for (let i = 1; i <= 26 && i * k <= m; i++) {
const l = i * k;
const cnt: number[] = new Array(26).fill(0);
for (let j = 0; j < l; j++) {
cnt[s.charCodeAt(j) - 'a'.charCodeAt(0)]++;
}
const freq: { [key: number]: number } = {};
for (const x of cnt) {
if (x > 0) {
freq[x] = (freq[x] || 0) + 1;
}
}
if (freq[k] === i) {
ans++;
}
for (let j = l; j < m; j++) {
const a = s.charCodeAt(j) - 'a'.charCodeAt(0);
const b = s.charCodeAt(j - l) - 'a'.charCodeAt(0);
freq[cnt[a]]--;
cnt[a]++;
freq[cnt[a]] = (freq[cnt[a]] || 0) + 1;
freq[cnt[b]]--;
cnt[b]--;
freq[cnt[b]] = (freq[cnt[b]] || 0) + 1;
if (freq[k] === i) {
ans++;
}
}
}
return ans;
};
let n = word.length;
let ans = 0;
for (let i = 0; i < n; ) {
let j = i + 1;
while (j < n && Math.abs(word.charCodeAt(j) - word.charCodeAt(j - 1)) <= 2) {
j++;
}
ans += f(word.substring(i, j));
i = j;
}
return ans;
}