题目:
题解:
class Solution:def expand(self, s, left, right):while left >= 0 and right < len(s) and s[left] == s[right]:left -= 1right += 1return (right - left - 2) // 2def longestPalindrome(self, s: str) -> str:end, start = -1, 0s = '#' + '#'.join(list(s)) + '#'arm_len = []right = -1j = -1for i in range(len(s)):if right >= i:i_sym = 2 * j - imin_arm_len = min(arm_len[i_sym], right - i)cur_arm_len = self.expand(s, i - min_arm_len, i + min_arm_len)else:cur_arm_len = self.expand(s, i, i)arm_len.append(cur_arm_len)if i + cur_arm_len > right:j = iright = i + cur_arm_lenif 2 * cur_arm_len + 1 > end - start:start = i - cur_arm_lenend = i + cur_arm_lenreturn s[start+1:end+1:2]