Basically, from Loop 1, Get desired size of 5,
Meaning all vowels and k cons
Then Maintain nextCons array tracking the next cons
That means when,
All strings between nextCons are coveres
ex
l x
aeiouqaaaaq
All Strings from x-l are covered by next loop
C++
class Solution {
public:
long long countOfSubstrings(string word, int k) {
long long ans = 0;
int n = word.size();
unordered_set<char> s = {'a', 'e', 'i', 'o', 'u'};
unordered_map<char, int> mp;
vector<int> nextCons(n);
nextCons[n-1] = n;
for(int i=n-2; i>=0; i--){
if(s.count(word[i+1])){
nextCons[i] = nextCons[i+1];
}
else{
nextCons[i] = i+1;
}
}
int con = 0;
int low, high;
for(low=0, high=0; high<word.size(); high++){
if(s.count(word[high])){
mp[word[high]]++;
// Vow
}
else{
con ++;
}
while(con > k){
char lowChar = word[low];
if(s.count(lowChar)){
if(mp[lowChar] == 1)
mp.erase(lowChar);
else
mp[lowChar]--;
}
else{
con --;
}
low++;
}
while(mp.size() == 5 and con == k){
ans += nextCons[high] - high;
char lowChar = word[low];
if(s.count(lowChar)){
if(mp[lowChar] == 1)
mp.erase(lowChar);
else
mp[lowChar]--;
}
else{
con --;
}
low++;
}
}
return ans;
}
};