Skip to content

Instantly share code, notes, and snippets.

@aljorhythm
Created May 24, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aljorhythm/f8b5478ca268c23ab38e0e30c0319343 to your computer and use it in GitHub Desktop.
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'
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