Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb"
解法:
class Solution:
# @return a string
def longestPalindrome(self, s):
res = ""
for i in xrange(len(s)):
# odd case, like "aba"
tmp = self.helper(s, i, i)
res = max(tmp,res,key=len)
# even case, like "abba"
tmp = self.helper(s, i, i + 1)
res = max(tmp,res,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]
print Solution().longestPalindrome('abcdcblkjkabcddcbas')