• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# clean dp

a guest Aug 22nd, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Solution:
2.
3.     def longestPalindrome(self, s: str) -> str:
4.
5.         n = len(s)
6.         if n < 1:
7.             return ''
8.
9.         longest_palindrome = s[0]
10.
11.         dp = [[False]*n for _ in range(n)]
12.
13.         for gap in range(n):
14.
15.             for start in range(n-gap):
16.
17.                 end = start + gap
18.
19.                 if start == end:
20.                     dp[start][end] = True
21.                     continue
22.
23.                 dp[start][end] = s[start] == s[end]
24.
25.                 if end - start > 1:
26.                     dp[start][end] &= dp[start+1][end-1]
27.
28.                 if dp[start][end] and (end - start + 1) > len(longest_palindrome):
29.                     longest_palindrome = s[start:end+1]
30.
31.         return longest_palindrome
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?