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

边栏推荐
- C语言用 printf 打印 《爱心》《火星撞地球》等,不断更新
- Testing grpc service with grpcui
- Hustoj SPJ example
- Quickly write MVVM code using source generators
- [C language series] - branch and loop statements
- Research Report on the recommended lithography industry in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report is continuously u
- Slot
- Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report
- How to insert pseudo code into word documents simply and quickly?
- 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)
猜你喜欢

5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue

Jenkins operation Chapter 5 trigger, configuration webhook, Jenkins parameterized construction
![[CV] wuenda machine learning course notes Chapter 13](/img/83/583d9ef852cf398ff8ed730bda0eab.jpg)
[CV] wuenda machine learning course notes Chapter 13
![[high concurrency] deeply analyze the callable interface](/img/42/43d1f0b894f2632f2c7f1bfe970708.jpg)
[high concurrency] deeply analyze the callable interface

2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously

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?
![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

How to use thread stack location

Research Report on the recommended lithography industry in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report is continuously u

Research Report on the new energy industry of recommended power equipment in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report
随机推荐
3 frequently tested SQL data analysis questions (including data and code)
Week 10 - task 3- from point to circle to cylinder
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
Tcapulusdb Jun · industry news collection (V)
5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue
Purple red solid meso tetra (o-alkoxyphenyl) porphyrin cobalt (meso-t (2-rop) PCO) / tetra (n, n-diphenyl-p-amino) phenyl porphyrin (tdpatph2)
DataX connection MySQL cannot find driver
Would like to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
IDENTITY
Slot
嵌入式RTOS
2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Stack -- 739 Daily temperature
The translation of those exquisite lines in the eighth season of the big bang
Spark saving to external data source
2022 recommended tire industry research report industry development prospect market analysis white paper
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)
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
C language uses printf to print love, Mars strikes the earth, etc., which are constantly updated