思路承接上一篇文章【leetcode】647. 回文子串(python,动态规划法;中心拓展法;暴力破解)
动态规划
class Solution:
def allPalindrome(self, s, max_len, max_str):
n = len(s)
dp = [[False for j in range(n)] for i in range(n)]
for i in range(n-1, -1, -1):
for j in range(i, n):
if s[i] == s[j] and (j - i <= 1 or dp[i + 1][j - 1]):
# print(s[i: j + 1])
dp[i][j] = True
if j - i + 1 >= max_len:
max_len = j - i + 1
max_str = s[i: j + 1]
return max_str
def longestPalindrome(self, s: str) -> str:
max_len = 0
max_str = ""
return self.allPalindrome(s, max_len, max_str)