leetcode 1371. 每个元音包含偶数次的最长子字符串 Python

  1. 每个元音包含偶数次的最长子字符串
    给你一个字符串 s ,请你返回满足以下条件的最长子字符串的长度:每个元音字母,即 ‘a’,‘e’,‘i’,‘o’,‘u’ ,在子字符串中都恰好出现了偶数次。

1 <= s.length <= 5 x 10^5
s 只包含小写英文字母。

示例 1:

输入:s = “eleetminicoworoep”
输出:13
解释:最长子字符串是 “leetminicowor” ,它包含 e,i,o 各 2 个,以及 0 个 a,u 。
示例 2:

输入:s = “leetcodeisgreat”
输出:5
解释:最长子字符串是 “leetc” ,其中包含 2 个 e 。
示例 3:

输入:s = “bcbcbc”
输出:6
解释:这个示例中,字符串 “bcbcbc” 本身就是最长的,因为所有的元音 a,e,i,o,u 都出现了 0 次。

class Solution:
    def findTheLongestSubstring(self, s: str) -> int:
        map_dict={'a':0,'e':0,'i':0,'o':0,'u':0}
        tmp={(0,0,0,0,0):-1}
        res=0
        for i,char in enumerate(s):
            if char in map_dict:
                map_dict[char]=(map_dict[char]+1)%2
                idx=tuple(map_dict.values())
                if idx in tmp:
                    res=max(res,i-tmp[idx])
                else:
                    tmp[idx]=i
            else:
                res=max(res,i-tmp[tuple(map_dict.values())])
                
        return res
so=Solution()
sa='eleetminicoworoep'
print(so.findTheLongestSubstring(sa))
class Solution:
    def findTheLongestSubstring(self, s: str) -> int:
        res=0
        status=0
        p=[-float('inf')]*32
        p[0]=-1
        map_dict={'a':0,'e':1,'i':2,'o':3,'u':4}
        for i in range(len(s)):
            if s[i] in map_dict:
                status^=(1<<map_dict[s[i]])
            if p[status] != -(float('inf')):
                res=max(res,i-p[status])
            else:
                p[status]=i
        return res
so=Solution()
sa='eleetminicoworoep'
print(so.findTheLongestSubstring(sa))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值