Maximum Number of Vowels in a Substring of Given Length LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0