Skip to content

Commit 465e6f8

Browse files
authored
Create maximum-number-of-vowels-in-a-substring-of-given-length.cpp
1 parent 628b547 commit 465e6f8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxVowels(string s, int k) {
7+
static const unordered_set<char> vowels{'a', 'e', 'i', 'o', 'u'};
8+
int result = 0, curr = 0;
9+
for (int i = 0; i < s.length(); ++i) {
10+
curr += vowels.count(s[i]);
11+
if (i >= k) {
12+
curr -= vowels.count(s[i - k]);
13+
}
14+
result = max(result, curr);
15+
}
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)