Python hashmap beats 99%


  • 0
    R
        def lengthOfLongestSubstring(self, st):
            if len(st) == 0:
                return 0
            hashy = {}
            i = 0
            j = 0
            mx = 0
            
            for k, v in enumerate(st):
                if v in hashy:
                    
                    j = j if j > hashy[v]+1 else hashy[v]+1
                    
                hashy[v] = k
                mx = mx if mx > i - j + 1 else i - j + 1
                i += 1
            return mx
    

  • 0
    F

    @randall.tom1868

    if len(s) == 0:
                return 0
            hasht = {}
            i = 0
            mx = 0
            for k, v in enumerate(s):
                if v in hasht:
                    i = i if i > hasht[v] + 1 else hasht[v] + 1
                hasht[v] = k
                mx = mx if mx > k - i + 1 else k - i + 1
            return mx
    

Log in to reply
 

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