-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPalindromicSubsequence.java
71 lines (59 loc) · 1.79 KB
/
LongestPalindromicSubsequence.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// https://leetcode.com/problems/longest-palindromic-subsequence/submissions/
// #dynamic-programming #lcs
/*
Input: s = "bbbab"
Output: 4
Explanation: One possible longest palindromic subsequence is "bbbb".
// Approach 1: indirect
// s
// s1 = s.reverse()
=> LCS(s, s1)
// Approach 2: direct
dp[i][j] = longest palindrome of S[i..j]
i -> k => dp[i][k-1] + 2 => (k, j)
idea 1: -> work but complicated
s[j] nam trong S[i...j-1]
=> dp[i][j] = max of - khong chon: dp[i][j-1],
- chon:
-> index: k1, k2... <= j-1 ma S[k..] = S[j]
dp[k1 + 1][j - 1] + 2 (chuoi doi xung: k1 ... j)
else
=> dp[i][j] = dp[i][j-1]
idea 2: s[i] == s[j] => ?
s[i] != s[j] => >
dp[i][i] = 1
dp[i][i + 1] = 2 if s[i] == s[i + 1] else 1
(**) => iterate: i desc, j acs; j > i
s[i] == s[j], j = i + 1
=> dp[i][j] = dp[i+1][j-1] + 2 (**)
s[i] != s[j]
=> dp[i][j] = Max(dp[i][j-1], dp[i+1][j])
*/
public class LongestPalindromicSubsequence {
public int longestPalindromeSubseq(String s) {
int n = s.length();
// dp[i][j] : max length of palindrome subsequence from i -> j,
int[][] dp = new int[n][n];
// base - note point
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
if (i < n - 1) {
if (s.charAt(i) == s.charAt(i + 1)) {
dp[i][i + 1] = 2;
} else {
dp[i][i + 1] = 1;
}
}
}
for (int i = n - 1; i >= 0; i--) { // note point: the way to iterate
for (j = i + 2; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
}
return dp[0][n - 1];
}
}