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 analyze the longest substring of non-repetitive characters in JS,PY,TS version

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to carry out the longest substring analysis of non-repetitive characters in JS,PY,TS version. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Description of item:

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.

Example 3:

Enter: "pwwkew"

Output: 3

Explanation: because the longest substring of non-repeating characters is "wke", its length is 3.

Please note that your answer must be the length of the substring. "pwke" is a subsequence, not a substring.

Problem-solving ideas

The first idea after seeing this question is to loop, and then use HashMap to record the non-repetitive characters collected each time, and then define a record to record the subscript at the beginning of the collection. The subscript update judgment is whether the current HashMap has traversed characters, but the latter thought, since it is a judgment, there is no need to apply for additional space, because I have the subscript of the record record, so I only need to judge the original characters. It just so happens that the indexOf of JS is the same as the index of PY, so you can choose the location where the query starts, so remove the HashMap and query the original array directly, which really takes up less space.

JS version

/ * * @ param {string} s * @ return {number} * / const lengthOfLongestSubstring = s = > {let len = s.length if (len {let len: number = s.length if (len int: "": type s: str: int "" if len (s) ")

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: 270

*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