-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLCS.java
More file actions
23 lines (22 loc) · 755 Bytes
/
Copy pathLCS.java
File metadata and controls
23 lines (22 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package dynamicProgramming.longestcommonsubsequence;
public class LCS {
public static void main(String[] args){
char[] x = {'A','L','I'};
char[] y = {'A','L','I','R','E','Z','A'};
int lcs = longestCommonSubsequence(x,y,x.length,y.length);
System.out.println(lcs);
}
public static int longestCommonSubsequence(char[] x, char[] y, int m, int n){
if( m == 0 || n == 0){
return 0;
}
if (x[m-1] == y[n -1]){
return longestCommonSubsequence(x,y,m-1,n-1) + 1;
}else{
int p = longestCommonSubsequence(x,y,m,n-1);
int q = longestCommonSubsequence(x,y,m -1,n);
int max = Math.max(p,q);
return max;
}
}
}