3. Longest Substring Without Repeating Characters
Solution
def lengthOfLongestSubstring(self, s: str) -> int:
seen = defaultdict(int)
left, right = 0, 0
answer = 0
while right < len(s) :
if s[right] in seen:
# need to take the max here because the left pointer may have moved past
# the index where the letter was previously seen. case: "abba"
left = max(left, seen[s[right]] + 1)
answer = max(answer, right - left + 1)
seen[s[right]] = right
right += 1
return answer
Test Cases
input
output
Last updated