求一列字符串中长度为k的子串最多有多少个元音字母,先预处理到第i个字符有多少个元音字母的累加和,再移动窗口扫一遍
1 #1456
2 #Runtime: 135 ms (Beats 83.33%)
3 #Memory: 18.1 MB (Beats 6.94%)
4
5 class Solution(object):
6 def maxVowels(self, s, k):
7 """
8 :type s: str
9 :type k: int
10 :rtype: int
11 """
12 cnt = [0] * (len(s) + 1)
13 t, ans = 0, 0
14 for i, ch in enumerate(s):
15 if ch in 'aeiou':
16 t += 1
17 cnt[i + 1] = t
18 for i in range(k, len(cnt)):
19 ans = max(ans, cnt[i] - cnt[i - k])
20 return ans