Skip to content

Longest Substring Without Repeating Characters

LeetCode

01 · Question

Given a string s, find the length of the longest substring without repeating characters.

02 · Solution

Reference solution

1def lengthOfLongestSubstring(s: str) -> int:
2 seen = {}
3 l = 0
4 res = 0
5
6 for r in range(len(s)):
7 if s[r] in seen and seen[s[r]] >= l:
8 l = seen[s[r]] + 1
9 seen[s[r]] = r
10 res = max(res, r - l + 1)
11
12 return res