Skip to content

Instantly share code, notes, and snippets.

@wanderindev
Created July 1, 2022 21:56
Show Gist options
  • Save wanderindev/6c7325cbef34ee6a450a751ed9b4c67a to your computer and use it in GitHub Desktop.
Save wanderindev/6c7325cbef34ee6a450a751ed9b4c67a to your computer and use it in GitHub Desktop.
class Solution(object):
def length_of_longest_substring(self, s):
"""
:type s: str
:rtype: int
"""
max_length = 0
chars = set()
left_index = 0
for right_index, char in enumerate(s):
while char in chars:
chars.remove(s[left_index])
left_index += 1
chars.add(char)
max_length = max(max_length, right_index - left_index + 1)
return max_length
# Test cases
sol = Solution()
assert sol.length_of_longest_substring("abcabcbb") == 3
assert sol.length_of_longest_substring("bbbbb") == 1
assert sol.length_of_longest_substring("pwwkew") == 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment