LeetCode12.29

Longest Substring Without Repeating Characters

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


Screen Shot 2018-12-25 at 10.15.52 PM.png

答案;

class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
dct = {}
max_so_far = curr_max = start = 0
for index, i in enumerate(s):
if i in dct and dct[i] >= start:
max_so_far = max(max_so_far, curr_max)
curr_max = index - dct[I]
start = dct[i] + 1
else:
curr_max += 1
dct[i] = index
return max(max_so_far, curr_max)

你可能感兴趣的:(LeetCode12.29)