Advertisement
smj007

Untitled

Feb 13th, 2024
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. class Solution:
  2.     def longestPalindrome(self, s: str) -> str:
  3.  
  4.         res = ""
  5.         n = len(s)
  6.         dp = [[False]*n]*n
  7.  
  8.         for j in range(n):
  9.             for i in range(j+1):
  10.                 if i==j:
  11.                     dp[j][i] = True
  12.                 elif j==i+1:
  13.                     dp[j][i] = (s[i] == s[j])
  14.                 else:
  15.                     dp[j][i] = (dp[j-1][i+1] and s[i]==s[j])
  16.  
  17.                 if len(res) < j-i+1 and dp[j][i]:
  18.                     res = s[i:j+1]
  19.  
  20.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement