Maximum Number of Vowels in a Substring of Given Length LeetCode solution in C++
1class Solution {2public:3bool checkVowel(char c) {4if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true;5return false;6}7int maxVowels(string s, int k) {8int count = 0;9int ans = 0;10for (int i = 0; i < k; i++) {11if (checkVowel(s[i])) count++;12}13ans = count;14int j = 0;15for (int i = k; i < s.size(); i++) {16if (checkVowel(s[i])) count++;17if (checkVowel(s[j])) count--;18j++;19ans = max(ans, count);20}21return ans;22}23};