Share my Python solution o(n)

  • 0
        def lengthOfLongestSubstring(self, s):
            :type s: str
            :rtype: int
            d = {}
            sub, st = 0, 0 
            #search window is [st..i]
            for i in range(len(s)):
                if s[i] in d:
                    st = max(d[s[i]]+1, st)
                sub = max(sub, i-st+1)
                d[s[i]] = i
            return sub

    pretty straightforward, just use search window and a variable to get current max substring length

Log in to reply

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