Python - 32ms solution


  • 0
    R
        def lengthOfLongestSubstring(self, s):
            """
            :type s: str
            :rtype: int
            """
            if len(s) <= 1:
                return len(s)
            this_substring = []
            max_so_far = 0
            for ch in s:
                if ch in this_substring:
                    if len(this_substring) > max_so_far:
                        max_so_far = len(this_substring)
                        #print "New max string is: %s" % this_substring
                    this_index = this_substring.index(ch)
                    this_substring = this_substring[this_index+1:]
                    this_substring.append(ch)
                else:
                    this_substring.append(ch)
            if len(this_substring) > max_so_far:
                max_so_far = len(this_substring)
                #print "New max string is: %s" % this_substring
            return max_so_far
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.