11-line python, 75ms runtime


  • 0
    R
    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            count = max_count = phase = 0
            chars = {}   
            for i in range(len(s)):
                if s[i] not in chars or chars[s[i]]<phase:
                    count+=1 
                else:
                    phase = chars[s[i]] + 1
                    count = i - phase  + 1
                if max_count<count: max_count = count 
                chars[s[i]] = i
            return max_count
    

Log in to reply
 

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