O(n) solution in python


  • 0
    C
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
      
        if len(s) is 0 or len(s) is 1:
            return len(s)
        len1 = 1
        temp = ''
        for i in range(len(s)):
            if s[i] in temp:
                start = s[:i].rindex(s[i])
                temp = s[start+1:i+1]
            else:
                temp += s[i]
            if len(temp) > len1:
                len1 = len(temp)
        return len1

Log in to reply
 

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