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

边栏推荐
- How to choose congestion model and anemia model
- 2022 recommended precious metal industry research report industry development prospect market analysis white paper (the attachment is a link to the online disk, and the report is continuously updated)
- 2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin (no2tdmpp) H2) /5,10,15,20-tetra (4-methylphenyl) porphyrin (TMPP) H2) Qiyue porphyrin products
- HTTP Caching Protocol practice
- [chromium] win10 vs2019 environment chromium configuration and compilation.
- Implementation of queue
- VLAN experiment
- The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
- 嵌入式RTOS
- 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
猜你喜欢

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

Why can't the article be posted?

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)

ES6 Modularization: export /import

Output various graphics and text on the console through C #

Pytest (7) -yield and termination function
![[CV] wuenda machine learning course notes Chapter 13](/img/83/583d9ef852cf398ff8ed730bda0eab.jpg)
[CV] wuenda machine learning course notes Chapter 13

Jenkins operation Chapter 5 trigger, configuration webhook, Jenkins parameterized construction

Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately

Design risc-v processor from scratch -- data adventure of five stage pipeline
随机推荐
Awk of shell script
5000+ word interpretation | Product Manager: how to do a good job in component selection?
Purple red solid meso tetra (o-alkoxyphenyl) porphyrin cobalt (meso-t (2-rop) PCO) / tetra (n, n-diphenyl-p-amino) phenyl porphyrin (tdpatph2)
2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up
Parsing rshub document auto generation API
Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
证券开户安全么,有没有什么危险呢
The translation of those exquisite lines in the eighth season of the big bang
Week 12 - task 2- shoulder to shoulder cadres
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Rich material libraries make modeling easy and efficient for developers
CodeIgniter active record not equal - CodeIgniter active record not equal
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
Common methods for describing 3D models of objects and their advantages and disadvantages
Love that can't be met -- what is the intimate relationship maintained by video chat
2022 recommended RCEP regional comprehensive economic partnership agreement market quotation Investment Analysis Industry Research Report (the attachment is a link to the online disk, and the report i
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?
Design risc-v processor from scratch -- data adventure of five stage pipeline
Establishing the development environment of esp8266
Leetcode theme [array] -219- duplicate Element II exists