Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
class Solution:
# @return a string
def getlongestpalindrome(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]
def longestPalindrome(self, s):
palindrome = ''
for i in range(len(s)):
str1 = self.getlongestpalindrome(s, i, i)
if len(str1) > len(palindrome): palindrome = str1
str2 = self.getlongestpalindrome(s, i, i + 1)
if len(str2) > len(palindrome): palindrome = str2
return palindrome