Created
October 8, 2024 03:04
-
-
Save hongtaoh/cfa35d261927cdffd427442ae002e716 to your computer and use it in GitHub Desktop.
Solution to Leetcode 3 Longest Substring Without Repeating Characters
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
class Solution: | |
def lengthOfLongestSubstring(self, s: str) -> int: | |
charSet = set() | |
left = 0 | |
maxLen = 0 | |
for right in range(len(s)): | |
while s[right] in charSet: | |
charSet.remove(s[left]) | |
left += 1 | |
charSet.add(s[right]) | |
maxLen = max(maxLen, len(charSet)) | |
return maxLen | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution has a higher complexity$O(n^2)$ because the $O(m)$ where m is the length of the substring.
in
operation indeque
takes