Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- memoized = {}
- curr_length = 0
- curr_pal = ""
- class Solution:
- def check_palindrome(self, str_):
- if len(str_) <= 1:
- return False
- global curr_length
- if len(str_) <= curr_length:
- return False
- if memoized.get(str_, None):
- return memoized[str_]
- if str_ == str_[::-1]:
- memoized[str_] = True
- curr_length = len(str_)
- return True
- memoized[str_] = False
- return False
- def longest_palindrome(self, str_):
- if len(str_) <= 1:
- return None
- n = len(str_)
- global curr_length
- if n <= curr_length:
- memoized[str_] = False
- return None
- if self.check_palindrome(str_):
- return str_
- n -= 1
- return(self.longest_palindrome(str_[:n]))
- def longestPalindrome(self, s):
- global curr_length
- global curr_pal
- for starting_index in range(len(s)):
- pal = self.longest_palindrome(s[starting_index:])
- if pal:
- if len(pal) > len(curr_pal):
- curr_pal = pal
- curr_length = len(curr_pal)
- return curr_pal
- solution = Solution()
- # print(solution.longestPalindrome("babadda"))
- print(solution.longestPalindrome("cbbd"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement