The difference between two adjacent characters is at most2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most2.
Return the number of complete substrings of word.
A substring is a non-empty contiguous sequence of characters in a string.
Example 1:
1 2
Input: word = "igigee", k = 2 Output: 3
Explanation: The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: igigee, igigee , igigee.
Example 2:
1 2
Input: word = "aaabbbccc", k = 3 Output: 6
Explanation: The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc.
classSolution { public: intcountCompleteSubstrings(string word, int k){ int n = word.size(); int res = 0; string_view w(word); for (int i = 0; i < n; ) { int start = i; for (i++; i < n && abs(w[i] - w[i - 1]) <= 2; i++) {} res += countSub(w.substr(start, i - start), k); } return res; }
intcountSub(string_view s, int k){ int n = s.size(); int res = 0; for (int i = 1; i <= 26 && i * k <= n; i++) { int right = 0, valid = 0; int count[26] = {0}; while (right < n) { int r = s[right] - 'a'; count[r]++; if (count[r] == k) { valid++; } if (right >= i * k - 1) { if (valid == i) { res++; } int l = s[right - i * k + 1] - 'a'; if (count[l] == k) { valid--; } count[l]--; } right++; } } return res; } };
classSolution { public: intcountCompleteSubstrings(string word, int k){ int res = 0; array<int, 26> m; for (int i = 1; i <= 26; i++) { int len = i * k, left = 0, right = 0, valid = 0; fill(m.begin(), m.end(), 0); while (right < word.size()) { int r = word[right] - 'a'; m[r]++; if (m[r] == k) { valid++; } if (right && abs(word[right - 1] - word[right]) > 2) { while (left < right) { int l = word[left++] - 'a'; if (m[l] == k) { valid--; } m[l]--; } } if (right - left == len - 1) { if (valid == i) { res++; } int l = word[left++] - 'a'; if (m[l] == k) { valid--; } m[l]--; } right++; } } return res; } };