forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
52 lines (50 loc) · 1.44 KB
/
Solution.cpp
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
class Solution {
public:
int countCompleteSubstrings(string word, int k) {
int n = word.length();
int ans = 0;
auto f = [&](string s) {
int m = s.length();
int ans = 0;
for (int i = 1; i <= 26 && i * k <= m; ++i) {
int l = i * k;
int cnt[26]{};
for (int j = 0; j < l; ++j) {
++cnt[s[j] - 'a'];
}
unordered_map<int, int> freq;
for (int x : cnt) {
if (x > 0) {
freq[x]++;
}
}
if (freq[k] == i) {
++ans;
}
for (int j = l; j < m; ++j) {
int a = s[j] - 'a';
int b = s[j - l] - 'a';
freq[cnt[a]]--;
cnt[a]++;
freq[cnt[a]]++;
freq[cnt[b]]--;
cnt[b]--;
freq[cnt[b]]++;
if (freq[k] == i) {
++ans;
}
}
}
return ans;
};
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && abs(word[j] - word[j - 1]) <= 2) {
++j;
}
ans += f(word.substr(i, j - i));
i = j;
}
return ans;
}
};