Python Solution O(n) and constant space


  • 0
    A

    class Solution(object):
    def lengthOfLongestSubstring(self, s):
    """
    :type s: str
    :rtype: int
    """
    first=0
    MaxL=0
    NewMax=0
    for i in range(len(s)):
    if s[i] not in s[first:i]:
    MaxL+=1
    else:
    newfirst=s[first:i].index(s[i])+1
    first+=newfirst
    MaxL=MaxL-newfirst+1
    NewMax=max(MaxL,NewMax)
    return NewMax


Log in to reply
 

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