python solution using string to contain the substring beats 100%


  • 1
    X
    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            """
            :type s: str
            :rtype: int
            """
            new_str = ''
            max = 0
            for ch in s:
                if not ch in new_str:
                    new_str += ch
                else:
                    max = len(new_str) if len(new_str) > max else max
                    idx = new_str.find(ch)
                    new_str = new_str[idx+1:] + ch
            max = len(new_str) if len(new_str) > max else max
            return max
    

  • 0
    N

    Isn't it quadratic solution?


Log in to reply
 

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