Skip to content

Commit 23692e2

Browse files
authored
Create 2953.Count-Complete-Substrings.cpp
1 parent 90a6b9d commit 23692e2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
public:
3+
int countCompleteSubstrings(string word, int k)
4+
{
5+
int n = word.size();
6+
int ret = 0;
7+
for (int i=0; i<n; )
8+
{
9+
int j = i+1;
10+
while (j<n && abs(word[j]-word[j-1])<=2)
11+
j++;
12+
ret += helper(word.substr(i, j-i), k);
13+
i = j;
14+
}
15+
return ret;
16+
}
17+
18+
bool check(vector<int>& freq, int k)
19+
{
20+
for (int x: freq)
21+
{
22+
if (x != k && x != 0)
23+
return false;
24+
}
25+
return true;
26+
}
27+
28+
int helper(string s, int k)
29+
{
30+
int count = 0;
31+
set<char>Set(s.begin(), s.end());
32+
for (int T = 1; T <= Set.size(); T++)
33+
{
34+
int length = T * k;
35+
vector<int>freq(26,0);
36+
int start = 0;
37+
int end = start + length - 1;
38+
for (int i = start; i <= min(end, (int)s.size() - 1); i++)
39+
{
40+
freq[s[i]-'a']++;
41+
}
42+
while (end < s.size())
43+
{
44+
if (check(freq, k)) count++;
45+
freq[s[start]-'a']--;
46+
start++;
47+
end++;
48+
if (end < s.size()) freq[s[end]-'a']++;
49+
}
50+
}
51+
52+
return count;
53+
}
54+
};

0 commit comments

Comments
 (0)