Skip to content

Instantly share code, notes, and snippets.

@ForgottenProgramme
Last active February 7, 2021 16:10
Show Gist options
  • Save ForgottenProgramme/4e3c583a3dd09596bc2fb9b890c47877 to your computer and use it in GitHub Desktop.
Save ForgottenProgramme/4e3c583a3dd09596bc2fb9b890c47877 to your computer and use it in GitHub Desktop.
//JAVA CODE TO FIND THE LONGEST COMMON SUBSEQUENCE
//MAHE IRAM KHAN
//18COB058
public class LongestCommonSubsequence {
int lcs(char[] X, char[] Y, int p, int q)
{
if (p == 0 || q == 0)
return 0;
if (X[p - 1] == Y[q - 1])
return 1 + lcs(X, Y, p - 1, q - 1);
else
return max(lcs(X, Y, p, q - 1), lcs(X, Y, p - 1, q));
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
public static void main(String[] args)
{
LongestCommonSubsequence lcs = new LongestCommonSubsequence();
String s1 = "AABGHSJKKKSHIEH";
String s2 = "AABCGDJHDHBJ";
char[] X = s1.toCharArray();
char[] Y = s2.toCharArray();
int p = X.length;
int q = Y.length;
System.out.println("Length of LCS is"
+ " " + lcs.lcs(X, Y, p, q));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment