-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path516 Longest Palindromic Subsequence.py
55 lines (43 loc) · 1.24 KB
/
516 Longest Palindromic Subsequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3
"""
Given a string s, find the longest palindromic subsequence's length in s. You
may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
Example 2:
Input:
"cbbd"
Output:
2
One possible longest palindromic subsequence is "bb".
Author: Rajeev Ranjan
"""
from collections import defaultdict
class Solution:
def longestPalindromeSubseq(self, s):
"""
Brute force 0-1, exponential
F[i][j] be the longest palindrom subseuence (not necessarily ending at
A[i] A[j])
F[i-1]F[j+1] = F[i][j] + 2 or F[i][j] # error
The transition function shoud be
F[i][j] = F[i+1][j-1] + 2 or F[i+1][j] or F[i][j-1]
:type s: str
:rtype: int
"""
n = len(s)
F = defaultdict(lambda: defaultdict(int))
for i in range(n):
F[i][i] = 1
for i in range(n-1, -1, -1):
for j in range(i+1, n):
F[i][j] = max(F[i+1][j], F[i][j-1])
if s[i] == s[j]:
F[i][j] = max(F[i][j], F[i+1][j-1] + 2)
return F[0][n-1]
if __name__ == "__main__":
assert Solution().longestPalindromeSubseq("bbbab") == 4