Created
July 1, 2022 21:56
-
-
Save wanderindev/6c7325cbef34ee6a450a751ed9b4c67a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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