Count Vowel Substrings of a String LeetCode solution in C++
1class Solution {2public:3bool isVowel(char c) {4return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';5};67int atMostK(string &s, int k) {8int res = 0, i = 0, n = s.size();9unordered_map<char, int> mp;1011for (int j = 0; j < n; j++) {12if (!isVowel(s[j])) {13i = j + 1;14// Clear map as new substring will begin15mp.clear();16continue;17}18mp[s[j]]++;19while (mp.size() > k) {20mp[s[i]]--;21if (mp[s[i]] == 0) mp.erase(s[i]);22i++;23}24res += j - i + 1;25}26return res;27}2829int countVowelSubstrings(string str) {30return atMostK(str, 5) - atMostK(str, 4);31}32};