Skip to content

Add Longest Common Subsequence algorithm (DP) #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Dynamic Programming/LongestCommonSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class LongestCommonSubsequence {

public static String getLCS(String str1, String str2) {

//At least one string is null
if(str1 == null || str2 == null)
return null;

//At least one string is empty
if(str1.length() == 0 || str2.length() == 0)
return "";

String[] arr1 = str1.split("");
String[] arr2 = str2.split("");

//lcsMatrix[i][j] = LCS of first i elements of arr1 and first j characters of arr2
int[][] lcsMatrix = new int[arr1.length + 1][arr2.length + 1];

for(int i = 0; i < arr1.length + 1; i++)
lcsMatrix[i][0] = 0;
for(int j = 1; j < arr2.length + 1; j++)
lcsMatrix[0][j] = 0;
for(int i = 1; i < arr1.length + 1; i++) {
for(int j = 1; j < arr2.length + 1; j++) {
if(arr1[i-1].equals(arr2[j-1])) {
lcsMatrix[i][j] = lcsMatrix[i-1][j-1] + 1;
} else {
lcsMatrix[i][j] = lcsMatrix[i-1][j] > lcsMatrix[i][j-1] ? lcsMatrix[i-1][j] : lcsMatrix[i][j-1];
}
}
}
return lcsString(str1, str2, lcsMatrix);
}

public static String lcsString (String str1, String str2, int[][] lcsMatrix) {
StringBuilder lcs = new StringBuilder();
int i = str1.length(),
j = str2.length();
while(i > 0 && j > 0) {
if(str1.charAt(i-1) == str2.charAt(j-1)) {
lcs.append(str1.charAt(i-1));
i--;
j--;
} else if(lcsMatrix[i-1][j] > lcsMatrix[i][j-1]) {
Copy link

@Santoshjonnakuti Santoshjonnakuti May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if lcsMatrix[i-1][j] is equal to lcsMatrix[i][j-1] then there will be more than 1 Longest Common Subsequence.
But in the above algorithm only one of the Longest Common Subsequence is printed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general algorithm of LCS tries to compute the length of the longest subsequence and print any of them.
Printing all the longest common subsequences is not common, that's why I kept it this way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay Thanks @rahul1995 bro!

i--;
} else {
j--;
}
}
return lcs.reverse().toString();
}

public static void main(String[] args) {
String str1 = "DSGSHSRGSRHTRD";
String str2 = "DATRGAGTSHS";
String lcs = getLCS(str1, str2);

//Print LCS
if(lcs != null) {
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
System.out.println("LCS: " + lcs);
System.out.println("LCS length: " + lcs.length());
}
}
}