Python Shortest O(n) and minimal Space Complexity - 85ms


  • 0
    D
    def lengthOfLongestSubstring(self, s):
            temp = result = ''
            for i in s:
                if i not in temp:
                    temp += i
                else:
                    index = temp.index(i)
                    temp = temp[index + 1: ] + i
                if len(result) < len(temp):
                    result = temp
            return len(result)
    

Log in to reply
 

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