n = len(s) if n == 1: maxPalindromeLen = 1 maxPalindrome = s return maxPalindrome
for i in range(n): for j in range(i, n): sub_str=s[i:j+1] if self.isPalindrome(sub_str): if j - i + 1 > maxPalindromeLen: maxPalindromeLen = j - i + 1 maxPalindrome = sub_str return maxPalindrome
defisPalindrome(self, s): n = len(s) # 根据回文的对称性,两个指针往中间靠拢 head = 0 tail = n - 1 while (head <= tail): if s[head] != s[tail]: returnFalse head += 1 tail -= 1