Created
April 2, 2021 14:59
-
-
Save prestigegodson/dc2b90888a1a66a1695878b2b7d9e387 to your computer and use it in GitHub Desktop.
Length of longest substring python
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: | |
def lengthOfLongestSubstring(self, s: str) -> int: | |
cursor = 0 | |
start = 0 | |
end = len(s) | |
longest = 0 | |
subStringMap = {} | |
while start < end: | |
character = s[start] | |
if character in subStringMap and subStringMap[character] >= cursor: | |
cursor = subStringMap[character] + 1; | |
subStringMap[character] = start | |
else: | |
longest = max(longest, (start + 1) - cursor) | |
subStringMap[character] = start | |
start = start + 1 | |
return longest | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment