This is my accepted Python version, what do you think of it?


  • 0
    Z
    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            max_len = 0
            subs = []
            for ch in s:
                if ch in subs:
                    max_len = max(len(subs), max_len)
                    subs = subs[subs.index(ch) + 1:]
                subs.append(ch)
            max_len = max(len(subs), max_len)
            return max_len

  • 0
    A

    In each iteration there is a sublist operation on subs, which costs O(n), which gives O(n^2) for total performance


  • 0
    J

    Yes, use a dictionary.


Log in to reply
 

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