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"
First, let's traverse the s, then expand each element, there are two possible situations, odd string or even string。
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
self.res=""
def helper(l,r):
while 0<=l<len(s) and 0<=r<len(s) and s[l]==s[r]:
tmp=s[l:r+1]
if len(tmp)>len(self.res):
self.res=tmp[:]
l-=1
r+=1
for i in range(len(s)):
l=r=i
helper(l,r)
l=i
r=i+1
helper(l,r)
return self.res