-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirrorstring.java
31 lines (29 loc) · 1.18 KB
/
mirrorstring.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
// Idea - if the lcs of str and its reverse str is same then it has a palindromic subsequence.
class Solution {
public int longestPalindromeSubseq(String s) {
String rev = new StringBuilder(s).reverse().toString();
return longestCommonSubsequence(s, rev);
}
public int longestCommonSubsequence(String text1, String text2) {
int n = text1.length();
int m = text2.length();
int[][] dp = new int[n + 1][m + 1];
// Bottom-up dynamic programming approach
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][m];
}
public static void main(String[] args) {
Solution solution = new Solution();
String testString = "bbbab"; // Example input
int result = solution.longestPalindromeSubseq(testString);
System.out.println("Longest Palindromic Subsequence Length: " + result);
}
}