Given a string, find the length of the oldest string that does not contain repeating characters.
Example:
Given "abcabcbb" that the oldest string without repeating characters is "abc" , then the length is 3.
Given "bbbbb" , the longest substring is, the "b" length is 1.
Given "pwwkew" that the eldest string is "wke" , the length is 3. Note that the answer must be a substring, "pwke" which is a subsequence and not a substring.
Answer:
First of all, the problem is to find the longest non-repeating substring, the non-repeating letters should be together with each other, the idea is to first initialize the definition of a longest non-empty string length of 1, followed by a substring, type string.
Iterates over the string given by the topic, encounters a non-repeating addition to the substring, if the letter exists in the substring, records the maximum length and starts with the next letter that repeats.
Attention:
Finally, don't forget to compare the substring to the maximum substring after the loop is finished.
Class solution (Object): def lengthoflongestsubstring (self, s): "" " : Type s:str : Rtype:int " "" if not s: return 0 substr= " longeststr=1 for S1 in S: if S1 not in substr: substr=substr +S1 Else: If Len (substr) >longeststr: longeststr=len (substr) substr=substr+s1 substr =substr[substr.index (S1) + 1:] If Len (substr) >longeststr: longeststr=len (substr) return Longeststr
Leetcode 3 python