Skip to content

Commit

Permalink
dp pallindromic subsequence pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhdeokar committed Sep 21, 2024
1 parent 5d370e6 commit 69f6146
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
55 changes: 40 additions & 15 deletions topics/topics/dp/lcs/LongestCommonSubsequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}
26 changes: 26 additions & 0 deletions topics/topics/dp/lcs/LongestPallindromicSubsequence.java
Original file line number Diff line number Diff line change
@@ -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){


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions topics/topics/dp/lcs/PrintLCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]) {
Expand All @@ -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];
}

}
Expand Down

0 comments on commit 69f6146

Please sign in to comment.