aboutsummaryrefslogtreecommitdiff
path: root/max_num_vowel_substr.cpp
blob: fe24499cf52addbaa36102870f8ad0ea7e14bb89 (plain)
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
// 1456. Maximum Number of Vowels in a Substring of Given Length
#include "leetcode.h"

/*
 * 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 u'
 */

class Solution {
public:
  int maxVowels(string s, int k) {
    int count = 0, counting = 0;
    unordered_set<char> bag = {'a', 'e', 'i', 'o', 'u'};
    for (int i = 0, local = 0; i < s.size(); i++) {
      local += bag.count(s[i]);
      if (i - k >= 0)
        local -= bag.count(s[i - k]);
      count = max(count, local);
    }
    return count;
  }
};

int main() {
  Solution obj;
  cout << obj.maxVowels("abciiidef", 3); // expect: 3
  cout << obj.maxVowels("aeiou", 2);     // expect: 2
  cout << obj.maxVowels("leetcode", 3);  // expect: 2
}