1456. Maximum Number of Vowels in a Substring of Given Length
Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.
Vowel letters in English are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { bool isVowel(char ch) { return ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'; } public: int maxVowels(string s, int k) { int res = 0, now = 0, n = s.length(), l = 0, r = 0; while(r < n) { if(isVowel(s[r++])) now++; if(r - l == k + 1 and isVowel(s[l++])) now--; res = max(res, now); } return res; } };
|