-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.java
More file actions
29 lines (24 loc) · 743 Bytes
/
Copy pathLCS.java
File metadata and controls
29 lines (24 loc) · 743 Bytes
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
package Codes.DynamicProgramming;
public class LCS {
static int lcs(String a, String b){
int m=a.length(), n=b.length();
int dp[][]= 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)
dp[i][j]=0;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(a.charAt(i-1)==b.charAt(j-1))
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[m][n];
}
public static void main(String[] args) {
String a="SATURDAY", b="SUNDAY";
System.out.println(lcs(a, b));
}
}