Medium
, Array/String
给定字符串,寻找不包含重复字符的最长子字符串。
Example:
'abcabcbb'的最长子字符串是'abc';
'bbb'的最长子字符串是’b'
Solution
建立字符的index字典,如果字符已经存在,跳过。
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
i, maxlen = 0, 0
charMap = {}
for j in xrange(len(s)):
if s[j] in charMap and i <= charMap[s[j]]:
i = charMap[s[j]] + 1
charMap[s[j]] = j
maxlen = max(j-i+1,maxlen)
return maxlen
```