I13-leetcode3 longest substring without repeating characters

Title description:

Algorithm: Sliding Window

Use a variable to store the substring in the window, if the current character appears in the window, update the window, intercept the position of the character that appears +1; if it does not appear, just add it directly to the window. During the sliding process, record the longest window length.
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        #方法一:滑动窗口
        ss=''#滑动窗口
        ans=0
        for i in s:
            if i not in ss:
                ss+=i
                ans=max(ans,len(ss))
            else:
                ss=ss[ss.index(i)+1:]+i
        return ans

Guess you like

Origin blog.csdn.net/m0_58086930/article/details/128627865