Last active
February 17, 2024 18:10
-
-
Save olegon/786c48fd2e9832ae17ed9c0a193701c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxVowels(string s, int k) const { | |
int local_max = 0; | |
int global_max = 0; | |
for (int i = 0; i < k; i++) { | |
if (isVowel(s, i)) local_max++; | |
global_max = max(local_max, global_max); | |
if (global_max == k) return global_max; | |
} | |
for (int i = k; i < s.size(); i++) { | |
if (isVowel(s, i)) local_max++; | |
if (isVowel(s, i - k)) local_max--; | |
global_max = max(local_max, global_max); | |
if (global_max == k) return global_max; | |
} | |
return global_max; | |
} | |
inline bool isVowel(string& s, int pos) const { | |
char c = s[pos]; | |
return c == 'a' | |
|| c == 'e' | |
|| c == 'i' | |
|| c == 'o' | |
|| c == 'u'; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment