-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromicSubsequence.java
52 lines (43 loc) · 1.43 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
package Dynamic_Programming;
public class LongestPalindromicSubsequence {
public static void fun(String s){
int n = s.length();
// reverse the string
StringBuilder sb = new StringBuilder(s);
sb.reverse();
String rs = sb.toString();
// find lcs of s and rs
int[][] dp = new int[n+1][n+1];
for (int i=1; i<dp.length; i++){
for (int j=1; j<dp[0].length; j++){
if (s.charAt(i-1) == rs.charAt(j-1)) {
dp[i][j] = 1 + dp[i-1][j-1];
}else{
int l1 = dp[i-1][j];
int l2 = dp[i][j-1];
dp[i][j] = Math.max(l1, l2);
}
}
}
int lcs = dp[n][n];
StringBuilder ans = new StringBuilder();
// printing the lcs
int i = n, j = n;
while (i>0 && j>0){
if (s.charAt(i-1) == rs.charAt(j-1)){
ans.append(s.charAt(i-1));
i = i-1;
j = j-1; // diagonal up
}else if (dp[i-1][j] >= dp[i][j-1]){
i = i-1;
}else{
j = j-1;
}
}
System.out.println("Length : " + lcs + "\nPalindrome : " + ans.reverse().toString());
}
public static void main(String[] args) {
String str = "bbabcbcab";
fun(str);
}
}