70-滑动窗口--LC1456. 定长子串中元音的最大数目

在这里插入图片描述

class Solution(object):
    def maxVowels(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        # 1.方法1也不知道对不对,在验证的时候超出时间限制了,
        # 没有方法2好,方法1是每次都要计算滑动窗里中元音的个数
        # n = len(s)
        # if not s or n == 0:
        #     return 0
        # i = 0
        # j = k

        # count = 0
        # while j <= n:
        #     total = s[i:j]
        #     res = 0
        #     for t in total:
        #         if t in {'a', 'e', 'i', 'o', 'u'}:
        #             res += 1
        #     count = max(count, res)
        #     i += 1
        #     j += 1
        # return count
        # 2
        n = len(s)
        if not s or n == 0:
            return 0
        set_ = set(['a', 'e', 'i', 'o', 'u'])
        res = 0
        count = 0
        # 先判断第一个k个字符串中的元音字母数
        for i in s[:k]:
            if i in set_:
                count += 1
        res = max(res, count)
        # 滑动窗口开始往后滑
        # 先判断排除的是否是元音,如果是-1
        # 再判断新加的是否是元音,如果是+1
        for i in range(k, n):
            if s[i-k] in set_:
                count -= 1
            if s[i] in set_:
                count += 1
            res = max(res, count)
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值