LeetCode12.29

作者: supermanwasd | 来源:发表于2018-12-29 22:18 被阅读0次

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

    Longest Substring Without Repeating Characters Given a st...

网友评论

    本文标题:LeetCode12.29

    本文链接:https://www.haomeiwen.com/subject/ejgqlqtx.html