Created
March 14, 2022 22:43
-
-
Save ericness/7e6641057ad4db6637d6980816044478 to your computer and use it in GitHub Desktop.
LeetCode 3 solution
This file contains hidden or 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
| from collections import defaultdict | |
| class Solution: | |
| def lengthOfLongestSubstring(self, s: str) -> int: | |
| """Count longest sequence with no repeating characters | |
| Args: | |
| s (str): String to analyze | |
| Returns: | |
| int: Length of longest sequence | |
| """ | |
| left, right = 0, 0 | |
| character_count = defaultdict(int) | |
| max_length = 0 | |
| if len(s) > 0: | |
| character_count[s[right]] += 1 | |
| while right < len(s): | |
| if all([val <= 1 for val in character_count.values()]): | |
| length = right - left + 1 | |
| if length > max_length: | |
| max_length = length | |
| right += 1 | |
| if right < len(s): | |
| character_count[s[right]] += 1 | |
| else: | |
| character_count[s[left]] -= 1 | |
| left += 1 | |
| return max_length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment