题解 | 包含不超过两种字符的最长子串

s=input()
#找出最多包含两种字符的最长子串 t
def long_str(s:str)->int:
    left=0
    n=len(s)
    res=0
    dic={}
    for right in range(n):
        if s[right] not in dic:
            dic[s[right]]=1
        else:
            dic[s[right]]+=1

        while len(dic)>2:
            dic[s[left]]-=1
            if dic[s[left]]==0:
                del dic[s[left]]
            left+=1
        res=max(res,right-left+1)
    return res                

print(long_str(s))




全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 17:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务