Python O(n) solution using deque beats 100%

  • 3
    from collections import deque
    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            :type s: str
            :rtype: int
            chars = deque()
            longest = 0
            for c in s:
                if c in chars:
                    length = len(chars)
                    if length > longest: 
                        longest = length
                    while chars.popleft() != c:
            return max(longest, len(chars))

Log in to reply

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