Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def longestPalindrome(self, s: str) -> str:
- if len(s) == 0:
- return ""
- is_palindrome = [[False for _ in range(len(s))] for _ in range(len(s))]
- longest_palindrome = s[0]
- for i in range(len(s)):
- is_palindrome[i][i] = True
- if i < len(s) - 1 and s[i] == s[i + 1]:
- is_palindrome[i][i + 1] = True
- longest_palindrome = s[i:i + 2]
- for length in range(2, len(s)):
- for st in range(len(s) - length):
- if is_palindrome[st + 1][st + length - 1] and s[st] == s[st + length]:
- is_palindrome[st][st + length] = True
- longest_palindrome = s[st:st + length + 1]
- return longest_palindrome
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement