Leetcodeブラシ問題(python)の重複文字のない最長男列

1159 ワード

文字列を指定して、重複文字を含まない最長子列の長さを見つけます.
例:
「abcabcbb」として与えられ、重複文字のない最長男列は「abc」であり、長さは3である.
「bbbbb」が与えられ、最も長いサブストリングは「b」であり、長さは1である.
「pwwkew」と与えられ、最長男列は「wke」で、長さは3である.答えはサブストリングでなければなりません.「pwke」はサブストリングではなくサブストリングです.
class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        oldL=0
        longL=0
        j=0
        
        if len(s) == 1:
            return 1
        
        while len(s) > 1:
            i=1
            while i < len(s):
                if s[i] not in s[:i]: #              ,   +1,       
                    i += 1
                else:
                    j = s[:i].index(s[i]) #              
                    oldL=i
                    if oldL > longL:
                        longL=oldL
                    break
            
            if i == len(s): #   i      s   ,      ,     
                oldL=i
                if oldL > longL:
                    longL=oldL
                break

            s=s[j+1:] #                     ,       
            
        return longL