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

边栏推荐
- 64 commonly used terms for data analysis, really all!
- Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
- Design risc-v processor from scratch -- data adventure of five stage pipeline
- Open source demo| you draw and I guess -- make your life more interesting
- 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)
- Robot reinforcement learning -- first person vs third person
- Awk of shell script
- Use VS to create a static link library Lib and use
- 机器人强化学习——第一人称 VS 第三人称
- 机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)
猜你喜欢

Internet enterprises need CRM software to help

PCI Verilog IP

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

How to insert pseudo code into word documents simply and quickly?

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)

The fresh student who was born in Ali after 2000: it's really fragrant to mend this

HTTP Caching Protocol practice

HTTP Caching Protocol practice

Creation of Arduino uno development environment

Conditional test, if and case conditional test statements of shell script
随机推荐
想问问,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
Kubernetes backup disaster recovery service product experience tutorial
Robot reinforcement learning -- first person vs third person
(practice C language every day) matrix
Internet enterprises need CRM software to help
3 frequently tested SQL data analysis questions (including data and code)
What has urbanization brought to our mental health and behavior?
51 lines of code, self-made TX to MySQL software!
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
2022 recommended tire industry research report industry development prospect market analysis white paper
DANGER! V** caught climbing over the wall!
Week 10 - task 0- execution process instance resolution of constructors and destructors
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
Structure training camp module II operation
Segment in Lucene
机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)
Establishing the development environment of esp8266
[CV] wuenda machine learning course notes Chapter 13
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
Open a new ecological posture | use WordPress remote attachment to store to Cos