Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 1456. Maximum Number of Vowels in a Substring of Given Length

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

Share this post on:

Previous
[LeetCode] 643. Maximum Average Subarray I
Next
[LeetCode] 定長 Sliding Window Pattern