Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequenceand not a substring.
翻译:寻找最长不重复的子串,使用一个hash表即可,i和j代表两个指针,不断的向前遍历
class Solution(object):
def lengthOfLongestSubstring(self, s):
cnt = len(s)
hash_str = {}
for i in range(256):
hash_str[chr(i)] = 0
i = 0
j = 0
max_length = 0
while(j<cnt):
if hash_str[s[j]] == 1:
max_length = max(j-i,max_length)
while(s[i]!=s[j]):
hash_str[s[i]] = 0
i+=1
i+=1
j+=1
else:
hash_str[s[j]] = 1
j+=1
max_length = max(max_length,cnt-i)
return max_length