Skip to content

Instantly share code, notes, and snippets.

@olegon
Last active February 17, 2024 18:10
Show Gist options
  • Save olegon/786c48fd2e9832ae17ed9c0a193701c5 to your computer and use it in GitHub Desktop.
Save olegon/786c48fd2e9832ae17ed9c0a193701c5 to your computer and use it in GitHub Desktop.
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