1456. Maximum Number of Vowels in a Substring of Given Length

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

class Solution {
public:
bool isVowel(char c)
{
return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
}
int maxVowels(string s, int k) {
int l=0, r= 0, n=s.size();
unordered_map<char, int> mp;
int mp_size = 0;
int ret = 0;
while(r<n)
{
char c = s[r++];
if(isVowel(c) ){
mp[c]++;
mp_size ++;
}
if(r-l== k)
{
ret = max(ret, mp_size);
char d = s[l++];
if(isVowel(d)){
mp[d]--;
mp_size--;
}
}
}
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)