forked from singhmayank980/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubs.java
More file actions
54 lines (38 loc) · 1.33 KB
/
Copy pathLongestCommonSubs.java
File metadata and controls
54 lines (38 loc) · 1.33 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
package CP.DP;
import java.util.Arrays;
public class LongestCommonSubs {
public static void main(String[] args) {
String s1 = "AGGTAB";
String s2 = "GXTXAYB";
char[] x = s1.toCharArray();
char[] y = s2.toCharArray();
int m = x.length;
int n = y.length;
StringBuilder s = new StringBuilder();
int[][] L = new int[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i == 0 || j == 0){
L[i][j] = 0;
}
else if(x[i-1] == y[j-1]){
L[i][j] = L[i-1][j-1] + 1;
if((L[i-1][j] == L[i][j] - 1) && (L[i][j-1] == L[i][j] - 1)) {
if(i==1 && j==5){
System.out.println(L[i][j-1] + " " + L[i][j]);
}
//System.out.println(i + " " + j);
s.append(x[i - 1]);
}
}
else{
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
}
System.out.println(Arrays.deepToString(L));
System.out.println(L[2][1]);
System.out.println(L[m][n]);
System.out.println(s);
}
}