diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/max_num_vowel_substr.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/max_num_vowel_substr.cpp')
-rw-r--r-- | other/max_num_vowel_substr.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/other/max_num_vowel_substr.cpp b/other/max_num_vowel_substr.cpp new file mode 100644 index 0000000..fe24499 --- /dev/null +++ b/other/max_num_vowel_substr.cpp @@ -0,0 +1,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 +} |