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 realize the longest substring of non-repeating characters in Java

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

Share

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

This article mainly explains "how to realize the longest substring of Java non-repetitive characters". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the longest substring of Java non-repetitive characters".

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 subsequence and not a substring.

Given a string, find the length of the longest substring that does not contain repeating characters.

Example:

Given "abcabcbb", the longest substring without repeating characters is "abc", so the length is 3.

Given "bbbbb", the longest substring is "b" and the length is 1.

Given "pwwkew", the longest substring is "wke" and the length is 3. Please note that the answer must be a substring. "pwke" is a subsequence, not a substring.

Idea: prepare a dict to store the latest location of each letter, and if there is a repetition, record the

Popular version

Class Solution (object): def lengthOfLongestSubstring (self S): temp = res =''for item in s: if item not in temp: temp + = item if len (temp) > len (res): res = temp else: I = temp.index (item) # find the index corresponding to the data If I = = len (temp)-1: # if the tail is found temp = item # repeat temp else: temp = temp [I + 1:] + item return len (res)

Here is the simplest version

Def lengthOfLongestSubstring (s): d = {} start = 0 ans = 0 for ifor c in enumerate (s): if c in d: start = max (start,d [c] + 1) # abba d [c] = I ans = max (ans) I-start + 1) return ansprint (lengthOfLongestSubstring ('pwwkew')) Thank you for your reading The above is the content of "how to achieve the longest substring of Java non-repetitive characters". After the study of this article, I believe you have a deeper understanding of how to achieve the longest substring of Java non-repetitive characters, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report