给你一个字符串 s,找到 s 中最长的回文子串。
class Solution:
def longestPalindrome(self, s: str) -> str:
# 两种判断条件
palindrome = ''
for i in range(len(s)):
# 有两种情况ABA和AA分情况讨论即可
len1 = len(self.getlongestpalindrome(s, i, i))
if len1 > len(palindrome):
palindrome = self.getlongestpalindrome(s, i, i)
len2 = len(self.getlongestpalindrome(s, i, i+1))
if len2 > len(palindrome):
palindrome = self.getlongestpalindrome(s, i, i+1)
return palindrome
def getlongestpalindrome(self, s, l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l+1: r]