题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param A string字符串
# @return int整型
#
class Solution:
def getLongestPalindrome(self , A: str) -> int:
ans = 1
if len(A) > 1 and A[0] == A[1]:
ans =2
def f1(i,j):
if i-j < 0 or i+j > len(A)-1:
return j*2-1
elif A[i-j] == A[i+j]:
return f1(i,j+1)
else:
return j*2-1
def f2(i,j):
if i-j < 0 or i+j+1 > len(A)-1:
return j*2
elif A[i-j] == A[i+j+1]:
return f2(i,j+1)
else:
return j*2
for i in range(1,len(A)-1):
if A[i-1] == A[i+1]:
ans = max(ans,f1(i,2))
if A[i] == A[i+1]:
ans = max(ans,f2(i,1))
print(ans)
return ans
