Last active
September 17, 2016 08:03
-
-
Save SumitJainUTD/38f1f74924c1f3014f13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package DynamicProgramming; | |
public class LongestCommonSubString { | |
public static int find(char [] A, char [] B){ | |
int [][]LCS = new int [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; | |
} | |
// if B is null then LCS of A, B =0 | |
for(int i=0;i<=A.length;i++){ | |
LCS[i][0]=0; | |
} | |
//fill the rest of the matrix | |
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; | |
}else{ | |
LCS[i][j] = 0; | |
} | |
} | |
} | |
int result = -1; | |
for(int i=0;i<=A.length;i++){ | |
for(int j=0;j<=B.length;j++){ | |
if(result<LCS[i][j]){ | |
result = LCS[i][j]; | |
} | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
String A = "tutorialhorizon"; | |
String B = "dynamictutorialProgramming"; | |
System.out.println("LCS length : " + find(A.toCharArray(), B.toCharArray())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment