Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the longest substring of non-repeating characters in Java

Shulou Source: shulou.com Published: 2022-06-01 16:30:51 10月04日 Update

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!

Tags: Longest character length that is learning content letters ideas simplicity popularity size position string tail sequence situation data article more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Shulou Technology MariaDB Docker