How to return the longest string length of non-repeating characters
This article mainly introduces "how to return the longest string length of non-repeating characters". In daily operation, I believe many people have doubts about how to return the longest string length of non-repeating characters. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to return the longest string length of non-repeating characters"! Next, please follow the editor to study!
Problem description
Given a string, please find out the length of the longest substring that does not contain repeating characters.
Example 1: enter: "abcabcbb"
Output: 3
Explanation: because the longest substring of non-repeating characters is "abc", its length is 3.
Example 2: enter: "bbbbb"
Output: 1
Explanation: because the longest substring of non-repeating characters is "b", its length is 1.
Solution
For this question, the meaning is very simple, "do not repeat" and "longest string" grasp these two key words.
So in a given string, compare the following and the preceding characters in a loop. For unordered comparison, we use the set () function to test the relationship, delete duplicate data, and calculate intersection, difference, union and so on.
Finally, the length of the non-repeating string is calculated with the len () function. For loops, if conditional statements, and the sorted () function are used in a series of procedures.
Next, show the complete code.
Def choose (msg):
Lst= []
For i in range (len (msg)):
For j in range (iMag1Len (msg) + 1):
M=msg [i:j]
If len (set (m)) = = len (m):
Lst.append (m)
Lst=list (sorted (lst,key=lambda x:len (x)
If len (lst) = = 0:
Return 0
Return len (lst [- 1])
Print (choose ("))
Let's run this code in the program.
Figure 2.1
At this point, the study on "how to return the longest string length of non-repeating characters" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!