无重复字符最长子串 python

303 阅读1分钟

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

输入: s = "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
class Solution:
    def length_of_substring(self,s:str)->int:
        occ=set()
        rk,ans=-1,0
        n=len(s)
        for i in range(n):
            if i!=0:
             occ.remove(s[i-1])
            while rk+1<n and s[rk+1] not in occ:
                occ.add(s[rk+1])
                rk+=1
            ans=max(ans,rk-i+1)
        return ans