Python O(n) 95 ms 9lines


  • 0
    J

    Here is my Python solution .How to improve it except using dict?

    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            """
            :type s: str
            :rtype: int
            """
            size = 0
            substring = []
            for i in range(len(s)):
                if s[i] in substring:
                    size = max(len(substring),size)
                    start_index = substring.index(s[i])
                    substring[:start_index+1]  = []    
                substring.append(s[i])
            return max(len(substring), size)  
    

Log in to reply
 

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