Skip to main content

3. Longest Substring Without Repeating Characters

https://leetcode.com/problems/longest-substring-without-repeating-characters

Python

Test Cases

  • "abcabcbb"
  • " "
  • "ok"
  • "dvdf"
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(s) < 2:
return len(s)

chars = set()
max_length, left, right = 0, 0, 0
while right < len(s):
if s[right] not in chars:
chars.add(s[right])
max_length = max(max_length, len(chars))
right += 1
continue
chars.remove(s[left])
left += 1

return max_length