Share my python solution, runtime: 74ms


  • 2
    C
    class Solution:
        # @return an integer
        def lengthOfLongestSubstring(self, s):
            ans = 0
            char_index = {}
            start = 0
            for i in range(0, len(s)):
                if s[i] in char_index and char_index[s[i]] >= start:
                    if ans < i - start:
                        ans = i - start
                    start = char_index[s[i]] + 1
                char_index[s[i]] = i
            if ans < len(s) - start:
                ans = len(s) - start
            return ans

Log in to reply
 

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