[translate by ChatGPT] This problem is really difficult. Even after reading the solution, it took me quite a while to realize that a two-pointer approach can be used to solve the “at least K” condition.
In the end, we can compute the number of substrings with exactly k by taking the difference between “at least k” and “at least k + 1”.
Code
class Solution {
public:
long long countOfSubstrings(string word, int k) {
return atLeast(word, k) - atLeast(word, k+1);
}
long atLeast(const string& word, const int& k) {
int n = word.size();
int left = 0;
int right = 0;
vector<int> cnt(6, 0);
long ans = 0;
while(right < n) {
++cnt[getIdx(word[right])];
while(cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0 && cnt[3] > 0 && cnt[4] > 0 && cnt[5] >= k) {
ans += (n - right);
--cnt[getIdx(word[left])];
++left;
}
++right;
}
return ans;
}
inline int getIdx(char c) {
switch(c) {
case 'a': return 0;
case 'e': return 1;
case 'i': return 2;
case 'o': return 3;
case 'u': return 4;
default : return 5;
}
}
};