3 - Longest Substring Without Repeating Characters
Written on October 21, 2015
Tweet
Given a string, find the length of the longest substring without repeating characters.
from collections import defaultdict
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
char_dict = defaultdict(int)
start = ret = 0
for i, c in enumerate(s):
char_dict[c] += 1
while char_dict[c] > 1:
char_dict[s[start]] -= 1
start += 1
ret = max(ret, i - start + 1)
return ret