My Python solution (O(n)?)


  • 2
    C

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

        substring = ""
        max_length = 0
        for c in s:
            if c not in substring:
                substring += c
            else:
                max_length = max(max_length,len(substring))
                substring = substring[substring.find(c)+1:] + c
        return max(max_length,len(substring))

Log in to reply
 

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