Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 682 Bytes

3_longest_substring_without_repeating_characters.md

File metadata and controls

29 lines (21 loc) · 682 Bytes

[Medium] 3. Longest Substring Without Repeating Characters

Question

[Medium] 3. Longest Substring Without Repeating Characters

Thought

用 sliding window 解。

Code

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        left = 0
        right = 0
        max_length = 0
        
        while right < len(s):
            if s[right] not in s[left:right]:
                right += 1
                max_length = max(max_length, right-left)
            else:
                left += 1
                right -= 1
        
        return max_length