Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. class Solution:
  2.     def longestPalindrome(self, s: str) -> str:
  3.        
  4.         if len(s) == 0:
  5.             return ""
  6.        
  7.         is_palindrome = [[False for _ in range(len(s))] for _ in range(len(s))]
  8.         longest_palindrome = s[0]
  9.        
  10.         for i in range(len(s)):
  11.             is_palindrome[i][i] = True
  12.             if i < len(s) - 1 and s[i] == s[i + 1]:
  13.                 is_palindrome[i][i + 1] = True
  14.                 longest_palindrome = s[i:i + 2]
  15.            
  16.         for length in range(2, len(s)):
  17.             for st in range(len(s) - length):
  18.                 if is_palindrome[st + 1][st + length - 1] and s[st] == s[st + length]:
  19.                     is_palindrome[st][st + length] = True
  20.                     longest_palindrome = s[st:st + length + 1]
  21.                    
  22.         return longest_palindrome
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement