-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLong_Substr.java
More file actions
32 lines (27 loc) · 785 Bytes
/
Long_Substr.java
File metadata and controls
32 lines (27 loc) · 785 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
30
31
32
public class Long_Substr{
public static int lcs(String s1, String s2){
int n = s1.length();
int m = s2.length();
int maxLength =0;
if(n == 0 || m == 0){
return 0;
}
int dp[][] = new int[n+1][m+1];
for(int i =1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(s1.charAt(i-1) == s2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
maxLength = Math.max(maxLength, dp[i][j]);
}else{
dp[i][j] = 0;
}
}
}
return maxLength;
}
public static void main(String[] args){
String s1 = "ABC";
String s2 ="AEC";
System.out.println(lcs(s1,s2));
}
}