Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created January 23, 2021 22:19
Show Gist options
  • Select an option

  • Save mvallebr/529b49bb849dc72d8aabfc9c0a1c417e to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/529b49bb849dc72d8aabfc9c0a1c417e to your computer and use it in GitHub Desktop.
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start = end = 0
current_chars = set()
length = max_length = 0
while end < len(s):
if s[end] not in current_chars:
current_chars.add(s[end])
length += 1
if length > max_length:
max_length = length
end += 1
else:
while start < end:
current_chars.remove(s[start])
length -= 1
start += 1
if s[start - 1] == s[end]:
break
return max_length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment