diff --git a/topics/topics/dp/lcs/LongestCommonSubsequence.java b/topics/topics/dp/lcs/LongestCommonSubsequence.java index 734850e..96e94e6 100644 --- a/topics/topics/dp/lcs/LongestCommonSubsequence.java +++ b/topics/topics/dp/lcs/LongestCommonSubsequence.java @@ -11,45 +11,70 @@ public static void main(String[] args) { LongestCommonSubsequence lcs = new LongestCommonSubsequence(); System.out.println(lcs.recursive(s1, s2)); System.out.println(lcs.topDown(s1, s2)); + + lcs.pallindromicCase(); + + } + + private void pallindromicCase() { + String s1 = "agbcba"; + StringBuilder stringBuilder = new StringBuilder(); + for (int i = s1.length() - 1; i >= 0; i--) { + stringBuilder.append(s1.charAt(i)); + } + String s2 = stringBuilder.toString(); + System.out.println("s1:" + s1); + System.out.println("s2:" + s2); + + int matches = topDown(s1, s2); + System.out.println(matches); + } public int recursive(String s1, String s2) { - return recursive(s1, s1.length() - 1, s2, s2.length() - 1); + return recursive(s1, s1.length(), s2, s2.length()); } - public int recursive(String x, int i, String y, int j) { - if (i < 0 || j < 0) { + private int recursive(String x, int i, String y, int j) { + if (i == 0 || j == 0) { return 0; } - if (x.charAt(i) == y.charAt(j)) { + if (x.charAt(i - 1) == y.charAt(j - 1)) { return 1 + recursive(x, i - 1, y, j - 1); } else { return Integer.max(recursive(x, i - 1, y, j), recursive(x, i, y, j - 1)); } } - public int topDown(String x, String y) { - int[][] dp = new int[x.length() + 1][y.length() + 1]; + public int[][] topDownReturnsArr(String x, String y){ + int xlen = x.length(); + int ylen = y.length(); + int[][] dp = new int[xlen + 1][ylen + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } + topDown(dp, x, xlen, y, ylen); + return dp; + } - return topDown(dp, x, x.length() - 1, y, y.length() - 1); + public int topDown(String x, String y) { + int[][] dp = topDownReturnsArr(x, y); + return dp[x.length()][y.length()]; } - private int topDown(int[][] dp, String x, int i, String y, int j) { - if (i < 0 || j < 0) return 0; + private int topDown(int[][] dp, String x, int xitr, String y, int yitr) { + if (xitr <= 0 || yitr <= 0) return 0; - if (dp[i][j] != -1) { - return dp[i][j]; + if (dp[xitr][yitr] != -1) { + return dp[xitr][yitr]; } - if (x.charAt(i) == y.charAt(j)) { - dp[i][j] = 1 + topDown(dp, x, i - 1, y, j - 1); + if (x.charAt(xitr - 1) == y.charAt(yitr - 1)) { + dp[xitr][yitr] = 1 + topDown(dp, x, xitr - 1, y, yitr - 1); } else { - dp[i][j] = Integer.max(topDown(dp, x, i - 1, y, j), topDown(dp, x, i, y, j - 1)); + dp[xitr][yitr] = Integer.max(topDown(dp, x, xitr - 1, y, yitr), topDown(dp, x, xitr, y, yitr - 1)); } - return dp[i][j]; + return dp[xitr][yitr]; } } diff --git a/topics/topics/dp/lcs/LongestPallindromicSubsequence.java b/topics/topics/dp/lcs/LongestPallindromicSubsequence.java new file mode 100644 index 0000000..198366a --- /dev/null +++ b/topics/topics/dp/lcs/LongestPallindromicSubsequence.java @@ -0,0 +1,26 @@ +package dp.lcs; + +import java.util.Collections; + +public class LongestPallindromicSubsequence { + + public static void main(String[] args) { + + String s1 = "agbcba"; + + StringBuilder stringBuilder = new StringBuilder(); + for (int i = s1.length() - 1; i >= 0; i--) { + stringBuilder.append(s1.charAt(i)); + } + String s2 = stringBuilder.toString(); + System.out.println("s1:"+s1); + System.out.println("s2:"+s2); + + } + + private void lcs(String s1, String s2){ + + + } + +} diff --git a/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java b/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java index 35ae054..31ad61f 100644 --- a/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java +++ b/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java @@ -6,7 +6,7 @@ public class MinimumInsertionDeletionToCreateString { public static void main(String[] args) { - String from = "abcde"; + String from = "abcdasdade"; String to = "acdef"; MinimumInsertionDeletionToCreateString min = new MinimumInsertionDeletionToCreateString(); String lcs = min.convertString(from, to); diff --git a/topics/topics/dp/lcs/PrintLCS.java b/topics/topics/dp/lcs/PrintLCS.java index aa55bf5..1cb5684 100644 --- a/topics/topics/dp/lcs/PrintLCS.java +++ b/topics/topics/dp/lcs/PrintLCS.java @@ -2,16 +2,16 @@ public class PrintLCS { public static void main(String[] args) { - String s1 = "aggtab"; - String s2 = "gxtxaby"; + String s1 = "agbcba"; + String s2 = "abcbga"; PrintLCS printLCS = new PrintLCS(); System.out.println(printLCS.topDown(s1, s2)); } public String topDown(String x, String y) { - int[][] dp = new int[x.length()][y.length()]; - topDown(dp, x, x.length() - 1, y, y.length() - 1); - return calculateLcsString(dp, x, x.length() - 1, y, y.length() - 1); + LongestCommonSubsequence lcs = new LongestCommonSubsequence(); + int[][] dp = lcs.topDownReturnsArr(x, y); + return calculateLcsString(dp, x, x.length(), y, y.length()); } private String calculateLcsString(int[][] dp, String x, int x1, String y, int y1) { @@ -24,10 +24,10 @@ private String calculateLcsString(int[][] dp, String x, int x1, String y, int y1 StringBuilder res = new StringBuilder(); - while (x1 >= 0 && y1 >= 0) { + while (x1 > 0 && y1 > 0) { - if (x.charAt(x1) == y.charAt(y1)) { - res.append(x.charAt(x1)); + if (x.charAt(x1-1) == y.charAt(y1-1)) { + res.append(x.charAt(x1-1)); x1 -= 1; y1 -= 1; } else if (dp[x1 - 1][y1] > dp[x1][y1 - 1]) { @@ -40,19 +40,19 @@ private String calculateLcsString(int[][] dp, String x, int x1, String y, int y1 return res.toString(); } - private int topDown(int[][] dp, String x, int i, String y, int j) { - if (i < 0 || j < 0) return 0; + private int topDown(int[][] dp, String x, int xitr, String y, int yitr) { + if (xitr == 0 || yitr == 0) return 0; - if (dp[i][j] != 0) { - return dp[i][j]; + if (dp[xitr][yitr] != 0) { + return dp[xitr][yitr]; } - if (x.charAt(i) == y.charAt(j)) { - dp[i][j] = 1 + topDown(dp, x, i - 1, y, j - 1); + if (x.charAt(xitr - 1) == y.charAt(yitr - 1)) { + dp[xitr][yitr] = 1 + topDown(dp, x, xitr - 1, y, yitr - 1); } else { - dp[i][j] = Integer.max(topDown(dp, x, i - 1, y, j), topDown(dp, x, i, y, j - 1)); + dp[xitr][yitr] = Integer.max(topDown(dp, x, xitr - 1, y, yitr), topDown(dp, x, xitr, y, yitr - 1)); } - return dp[i][j]; + return dp[xitr][yitr]; } }