forked from ashwinp301/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
56 lines (45 loc) · 1.41 KB
/
Copy pathLongestCommonSubsequence.java
File metadata and controls
56 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class LongestCommonSubsequence {
public static int lcs(String s1,String s2,int n,int m) {
if(n==0 || m==0)
return 0;
if(s1.charAt(n-1)==s2.charAt(m-1))
return 1+lcs(s1,s2,n-1,m-1);
return Math.max(lcs(s1,s2,n-1,m),lcs(s1,s2,n,m-1));
}
public static int lcsTopDown(String s1,String s2,int n,int m,Integer[][] ar) {
if(n==0||m==0)
return 0;
if(ar[n][m] == null) {
if(s1.charAt(n-1)==s2.charAt(m-1))
ar[n][m] = 1+lcsTopDown(s1,s2,n-1,m-1,ar);
else
ar[n][m]= Math.max(lcsTopDown(s1,s2,n-1,m,ar),lcsTopDown(s1,s2,n,m-1,ar));
}
return ar[n][m];
}
public static int lcsBottomUp(String s1,String s2,int n,int m) {
if(n==0 || m==0)
return 0;
int[][] ar = new int[n+1][m+1];
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(s1.charAt(i-1)==s2.charAt(j-1)) {
ar[i][j]=1+ar[i-1][j-1];
}
else {
ar[i][j]=Math.max(ar[i][j-1],ar[i-1][j]);
}
}
}
return ar[n][m];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="ecfbefdcfca";
String s2="badfcbebbf";
System.out.println(LongestCommonSubsequence.lcs(s1, s2,s1.length(),s2.length()));
Integer[][] arr = new Integer[s1.length()+1][s2.length()+1];
System.out.println(LongestCommonSubsequence.lcsTopDown(s1, s2,s1.length(),s2.length(), arr));
System.out.println(LongestCommonSubsequence.lcsBottomUp(s1, s2,s1.length(),s2.length()));
}
}