Python - no need to use fancy data structure


  • 0
    M

    Since ascii representation of a character is bounded within [0,255], an list suffices.

    class Solution(object):
    
        def lengthOfLongestSubstring(self, s):
            """
            :type s: str
            :rtype: int
            """
            start = 0
            curr  = 0
            longest = 0
            
            a = [-1]*256
            i = 0
            while ( i < len(s) ):
                c = s[i]
                if a[ord(c)] >= start:
                    longest = max(longest, i-start)
                    start = a[ord(c)] + 1
                a[ord(c)] = i
                i += 1
            longest = max(longest, len(s)-start)
            return longest
    

Log in to reply
 

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