Python 79ms O(n) solution


  • 0
    H

    '''
    class Solution(object):
    def lengthOfLongestSubstring(self, s):
    """
    :type s: str
    :rtype: int
    """

        usedChar = {}
        start, maxLength = 0, 0
        
        for i in range(len(s)):
            if s[i] in usedChar and start <= usedChar[s[i]]:
                maxLength = max(maxLength, i - start)
                start = usedChar[s[i]] + 1
            usedChar[s[i]] = i
            
        return max(maxLength, len(s) - start)    
    

    '''


Log in to reply
 

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