【leetcode 5417. 定长子串中元音的最大数目】 Python 解题思路

题目链接:

解题思路:
用一个数组存储前面所有字符中元音的个数

class Solution:
    def maxVowels(self, s: str, k: int) -> int:
        yuanyin=['a','e','i','o','u']
        n=len(s)
        max_val=0
        if k==1:
            for i in range(n):
                if s[i] in yuanyin:
                    max_val=1
                    break
        else:
            dp=[0]
            for i in range(n):
                if s[i] in yuanyin:
                    dp.append(dp[-1]+1)
                else:
                    dp.append(dp[-1])
            dif=[dp[i+k]-dp[i] for i in range(n-k+1)]
            max_val=max(dif)
        return max_val
                

你可能感兴趣的:(leetcode,leetcode,python)