1456. Maximum Number of Vowels in a Substring of Given Length 發表於 2023-12-21 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526272829303132class 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)