Description
Given a string, find the length of the longest substring without repeating characters.
Solution
维护一个lp, rp和mp,mp表示s(lp,rp]中所有字母-位置的dict,双指针扫描即可
Code
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
lp = -1
mp = {}
for rp in range(0, len(s)):
if s[rp] in mp:
nlp = mp[s[rp]]
for i in range(lp + 1, nlp + 1):
mp.pop(s[i])
lp = nlp
mp[s[rp]] = rp
res = max(res, rp - lp)
return res