3. Longest Substring Without Repeating Characters
Given a string s
, find the length of the longest substring without repeating characters.
def lengthOfLongestSubstring(self, s: str) -> int:
max_len = 0
left, right = 0, 0
last_seen = {}
while right < len(s):
ch = s[right]
if ch in last_seen and last_seen[ch] >= left:
left = last_seen[ch] + 1
last_seen[ch] = right
max_len = max(max_len, right-left + 1)
right += 1
return max_len
Comments
Post a Comment