class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s)==1:
return s
long=1
final_left=-1
final_right=1
for i in range(1,len(s)-1):
left=i
right=i
while s[left]==s[right]:
left-=1
right+=1
if left<0 or right>len(s)-1:
break
if right-left-1>long:
long=right-left-1
final_left=left
final_right=right
for i in range(len(s)-1):
if s[i]==s[i+1]:
left=i
right=i+1
while s[left]==s[right]:
left=left-1
right+=1
if left<0 or right>len(s)-1:
break
if right-left-1>long:
long=right-left-1
final_left=left
final_right=right
return s[final_left+1:final_right]
网友评论