Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
Problem-Solving ideas: Use the Find function to find out if there is already a letter in the string to find, if so, then choose to count the new string from the next letter in the string to find the letter, of course, at this time to determine whether the string you have looked for is longer than the existing string. Also take care to handle the last found string.
Eg:s= "Echocomeon" to find the string tmp= "Echoc", when looking for s[5]= ' o ', there is already ' o ' in the string tmp, check len (TMP) and the length of the existing string, and update the new string tmp= "co".
1 classSolution:2 #@return An integer3 deflengthoflongestsubstring (self, s):4Length=Len (s)5 if(length==0):6 return07 elif(length==1):8 return19 Else:Ten #Work with the data one by one Onetmp="" Amax_len=0 -str_len="" - forIinchRange (length): theindex=Tmp.find (s[i]) - if(index>-1): - if(LEN (TMP) <Max_len): -tmp=tmp[(index+1):]+S[i] + Else: -max_len=Len (TMP) +tmp=tmp[(index+1):]+S[i] A Else: attmp=tmp+S[i] - if(LEN (TMP) >Max_len): -max_len=Len (TMP) - Else: - Pass - returnMax_len
Leetcode longest Substring without repeating characters python