Scribbling

LeetCode: 3. Longest Substring Without Repeating Characters 본문

Computer Science/Coding Test

LeetCode: 3. Longest Substring Without Repeating Characters

focalpoint 2021. 8. 11. 17:23
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        char_set = set()
        l = 0
        max_len = 0
        for r in range(len(s)):
            if s[r] in char_set:
                while s[r] in char_set:
                    char_set.remove(s[l])
                    l += 1
            char_set.add(s[r])
            max_len = max(max_len, r - l + 1)
        return max_len