Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 27, 2018 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/6a3a5c742384f6da868a2269a84e9299 to your computer and use it in GitHub Desktop.
Save thmain/6a3a5c742384f6da868a2269a84e9299 to your computer and use it in GitHub Desktop.
public class LongestCommonSubsequence {
public static int find(char[] A, char[] B) {
int[][] LCS = new int[A.length + 1][B.length + 1];
String[][] solution = new String[A.length + 1][B.length + 1];
// if A is null then LCS of A, B =0
for (int i = 0; i <= B.length; i++) {
LCS[0][i] = 0;
solution[0][i] = "0";
}
// if B is null then LCS of A, B =0
for (int i = 0; i <= A.length; i++) {
LCS[i][0] = 0;
solution[i][0] = "0";
}
for (int i = 1; i <= A.length; i++) {
for (int j = 1; j <= B.length; j++) {
if (A[i - 1] == B[j - 1]) {
LCS[i][j] = LCS[i - 1][j - 1] + 1;
solution[i][j] = "diagonal";
} else {
LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]);
if (LCS[i][j] == LCS[i - 1][j]) {
solution[i][j] = "top";
} else {
solution[i][j] = "left";
}
}
}
}
// below code is to just print the result
String x = solution[A.length][B.length];
String answer = "";
int a = A.length;
int b = B.length;
while (x != "0") {
if (solution[a][b] == "diagonal") {
answer = A[a - 1] + answer;
a--;
b--;
} else if (solution[a][b] == "left") {
b--;
} else if (solution[a][b] == "top") {
a--;
}
x = solution[a][b];
}
System.out.println(answer);
for (int i = 0; i <= A.length; i++) {
for (int j = 0; j <= B.length; j++) {
System.out.print(" " + LCS[i][j]);
}
System.out.println();
}
return LCS[A.length][B.length];
}
public static void main(String[] args) {
String A = "ACBDEA";
String B = "ABCDA";
System.out.println("LCS :" + find(A.toCharArray(), B.toCharArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment