[LeetCode Python3] 3. Longest Substring Without Repeating Characters

python3 Solution:

from collections import defaultdict
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        window = defaultdict(int)
        
        left, right = 0, 0
        nlen = 0
        
        while right < len(s):
            ch = s[right]
            right += 1
            window[ch] += 1
            while window[ch] > 1:
                d = s[left]
                left += 1
                window[d] -= 1
            nlen = max(nlen, right-left)
        return nlen
                

你可能感兴趣的:(LeetCode每日一题)