5. Longest Palindromic Substring
python
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s)<=1:return s
ansl=1
anss=s[0]
dp=[[False for i in range(len(s))] for j in range(len(s))]
for i in range(len(s)):
dp[i][i]=True
for j in range(i):
if s[i]==s[j] and (i-j<=2 or dp[j+1][i-1]):
dp[j][i]=True
if i+1-j>ansl:
ansl=i+1-j
anss=s[j:i+1]
return anss
枚举起点终点位置