当前位置:网站首页>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

边栏推荐
- 2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
- β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue
- HTTP Caching Protocol practice
- The first commercial spacewalk of mankind is finalized! Musk SpaceX announced a new round of space travel plan, and the American rich became repeat customers
- CodeIgniter active record not equal - CodeIgniter active record not equal
- 证券开户安全么,有没有什么危险呢
- Plugin
- [CV] wuenda machine learning course notes Chapter 13
猜你喜欢

The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology

VLAN experiment

Open a new ecological posture | use WordPress remote attachment to store to Cos

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

HTTP Caching Protocol practice

Quickly write MVVM code using source generators

Modularization and modular specification commonjs

ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers

The fresh student who was born in Ali after 2000: it's really fragrant to mend this
![[chromium] win10 vs2019 environment chromium configuration and compilation.](/img/20/428e6b22ed6955a732dd14d5ff0e3d.jpg)
[chromium] win10 vs2019 environment chromium configuration and compilation.
随机推荐
patent filter
Personal blog item: processing of reading number +1 after viewing article details
Week 10 - task 3- from point to circle to cylinder
Is it safe to open a securities account? Is there any danger
Use of sed in shell script
Parsing rshub document auto generation API
2022 recommended REITs Industry Research Report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously updated)
IDENTITY
Mongodb basic knowledge summary
Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code
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
Test content
[high concurrency] deeply analyze the callable interface
Programming specification and variables of shell script
2022 recommended quantum industry research industry development planning prospect investment market analysis report (the attachment is a link to the online disk, and the report is continuously updated
Ctrip launched the "3+2" office mode. Are you sour?
The first commercial spacewalk of mankind is finalized! Musk SpaceX announced a new round of space travel plan, and the American rich became repeat customers
Analysis report on the investment market situation of the development planning prospect of the recommended chip industry research industry in 2022 (the attachment is a link to the network disk, and th
Pytest (7) -yield and termination function
Embedded RTOS