1456.Maximum Number of Vowels in a Substring of Given Length
My code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def maxVowels(self, s: str, k: int) -> int:
vowels = {'a','e','i','o','u'}
ans = 0
count = 0
for c in s[:k]:
if c in vowels:
ans += 1
count += 1
for i in range(len(s) - k):
if s[i + k] in vowels:
count += 1
if s[i] in vowels:
count -= 1
ans = max(ans,count)
return ans
1
2
Runtime : 110ms (beats 94.62%)
Memory : 17.18MB (beats 69.84%)