当前位置:网站首页>Leecode3. Longest substring without repeated characters

Leecode3. Longest substring without repeated characters

2022-07-07 13:33:00 Miaomiao boss

 Insert picture description here

class Solution:
    def lengthOfLongestSubstring(s: str) -> int:
        #  Hash set , Record whether each character appears 
        occ = set()
        n = len(s)
        #  Right pointer , The initial value is  -1, It's like we're on the left side of the left bound of the string , It's not moving yet 
        rk, ans = -1, 0
        for i in range(n):
            if i != 0:
                #  The left pointer moves one space to the right , Remove a character 
                occ.remove(s[i - 1])
            while rk + 1 < n and s[rk + 1] not in occ:
                #  Keep moving the right pointer 
                occ.add(s[rk + 1])
                rk += 1
            #  The first  i  To  rk  A character is a very long non repeating character substring 
            ans = max(ans, rk - i + 1)
        print(ans)

s=Solution.lengthOfLongestSubstring("pwwkew")

原网站

版权声明
本文为[Miaomiao boss]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071055176098.html