File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Two_Pointers/2953.Count-Complete-Substrings Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments