JuniorJunior (1–3 yrs)CodingPythonAmazonMicrosoftAdobe
Longest substring without repeating
Find the length of the longest substring without repeating characters.
Answers use simple, clear English.
Time: O(n)Space: O(min(n, alphabet))
Quick interview answer
Sliding window with a map/set of last seen indices. Expand right; when duplicate found, move left past previous occurrence. Track max window length.
Detailed answer
Sliding window with a map/set of last seen indices. Expand right; when duplicate found, move left past previous occurrence. Track max window length.
Code example
def length_of_longest_substring(s: str) -> int:
last = {}
left = best = 0
for right, ch in enumerate(s):
if ch in last and last[ch] >= left:
left = last[ch] + 1
last[ch] = right
best = max(best, right - left + 1)
return bestPractice code · python (view only · no execution)
def length_of_longest_substring(s: str) -> int:
last = {}
left = best = 0
for right, ch in enumerate(s):
if ch in last and last[ch] >= left:
left = last[ch] + 1
last[ch] = right
best = max(best, right - left + 1)
return bestjuniormidsenior#sliding-window#string#blind75