Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to return the longest string length of non-repeating characters

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report