Concise and Simple Python O(n)


  • 0
        def lengthOfLongestSubstring(self, s):
    
            mx = i = j = 0
            chars = {}
            while i < len(s):
                if chars.get(s[i],0) > 0: #shrinking so no need to check for max length
                    chars[s[j]] -= 1
                    j += 1
                else:
                    mx = max(mx, i - j + 1)
                    chars[s[i]], i = 1, i + 1
            return mx
    

Log in to reply
 

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