当前位置:网站首页>Longest substring between two identical characters of leetcode simple question
Longest substring between two identical characters of leetcode simple question
2022-06-29 05:53:00 【·Starry Sea】
subject
Give you a string s, Please return The length of the longest substring between two identical characters , The length is calculated without these two characters . If there is no such substring , return -1 .
Substring Is a sequence of consecutive characters in a string .
Example 1:
Input :s = “aa”
Output :0
explain : The optimal substring is two ‘a’ Empty substring between .
Example 2:
Input :s = “abca”
Output :2
explain : The optimal substring is “bc” .
Example 3:
Input :s = “cbzxy”
Output :-1
explain :s There is no character that appears twice in , So back -1 .
Example 4:
Input :s = “cabbac”
Output :4
explain : The optimal substring is “abba” , Other non optimal solutions include “bb” and “” .
Tips :
1 <= s.length <= 300
s Only lowercase English letters
source : Power button (LeetCode)
Their thinking
This problem can start with the character frequency . Count the frequency of characters , If the frequency is greater than or equal to 2, It means that , There may be a substring in the middle of this character ; If the frequency is exactly equal to 2, Then the generated string is only one , If exceeded 2 Then there will be multiple strings , But the longest string is the string between the first occurrence and the last occurrence , So we can start from both ends to search for the position of the same character , And calculate the length of the current longest string .
class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
freq=Counter(s)
MAX=-1
for i,j in freq.items():
if j>1:
left=s.index(i) # First occurrence
right=len(s)-s[::-1].index(i)-1# The last location
if right-left-1>MAX: # Maximum string length update
MAX=right-left-1
return MAX

边栏推荐
- 2022 community group buying industry research industry development planning prospect investment market analysis report (the attachment is the online disk link, and the report is continuously updated)
- β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue
- The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
- IDENTITY
- JS messagechannel transport
- HTTP Caching Protocol practice
- In 2022, I haven't found a job yet. I have been unemployed for more than one year. What is the "old tester" for eight years?
- patent filter
- Implementation of queue
- Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
猜你喜欢

Analysis report on the investment market of the development planning prospect of the recommended wind power industry research industry in 2022 (the attachment is a link to the network disk, and the re

PCI Verilog IP

Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code

使用VS创建静态链接库.lib并使用

Why Houdini made the pyside2 plug-in crash

How to use thread stack location

Difference between parametric continuity and geometric continuity
![ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience](/img/fd/4c24e10fc91a7ce7e709a0874ba675.jpg)
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience

Programming specification and variables of shell script

Test content
随机推荐
Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code
2022-01 Microsoft vulnerability notification
QT precautions and RCC download address
PCI Verilog IP
Tcapulusdb Jun · industry news collection (III)
The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
Common methods for describing 3D models of objects and their advantages and disadvantages
机器人强化学习——第一人称 VS 第三人称
Personal blog item: processing of reading number +1 after viewing article details
Use some examples of qte5
Easy to get started naturallanguageprocessing series topic 7 text classification based on fasttext
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
RTOS embarqués
Can use the mouse, will reinstall the computer system tutorial sharing
There are two ways for golang to develop mobile applications
5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
Mongodb basic knowledge summary
Test content
HTTP Caching Protocol practice
IDENTITY