Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd" Output: "bb"
class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ res = "" # 遍历字符串,以单字符或双字符为中心,向两边扩展,保留最大长度 for i in xrange(len(s)): odd = self.helper(s, i, i) even = self.helper(s, i, i+1) res = max(res, odd, even, key=len) return res def helper(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]