Created
May 24, 2020 16:11
-
-
Save aljorhythm/f8b5478ca268c23ab38e0e30c0319343 to your computer and use it in GitHub Desktop.
Maximum number of vowel letters in any substring of string 's' with length 'k'
This file contains hidden or 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(object): | |
def maxVowels(self, s, k): | |
""" | |
:type s: str | |
:type k: int | |
:rtype: int | |
""" | |
maxCount = 0 | |
lastCount = -1 | |
lastSubstrFirstLetterIsVowel = False | |
isVowel = lambda letter: letter in ['a','e','i','o','u'] | |
for i in range(len(s)): | |
if i + k > len(s): | |
break | |
vowCount = 0 | |
if lastCount == -1: | |
substr = s[i:i+k] | |
vowCount = len(filter(isVowel, substr)) | |
else: | |
vowCount = lastCount - (1 if lastSubstrFirstLetterIsVowel else 0) + isVowel(s[i+k-1]) | |
lastSubstrFirstLetterIsVowel = isVowel(s[i]) | |
maxCount = max(vowCount, maxCount) | |
lastCount = vowCount | |
return maxCount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment