forked from the-moonLight0/Hactober-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.java
32 lines (29 loc) · 1016 Bytes
/
LCS.java
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
import java.util.*;
import java.lang.*;
import java.io.*;
class LCS{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter First String");
String s1=sc.next(); // Take both the string as input
System.out.println("Enter second String");
String s2=sc.next();
System.out.println("length of longest subsequence present in both of them "+lcs(s1.length(), s2.length(), s1, s2));
}
static int lcs(int p, int q, String s1, String s2){
// your code here
char[] m = s1.toCharArray();
char[] n = s2.toCharArray();
int t[][] = new int[p+1][q+1];
for(int i=1;i<p+1;i++){
for(int j=1;j<q+1;j++){
if(m[i-1]==n[j-1]){
t[i][j] = 1 + t[i-1][j-1];
}else{
t[i][j] = Math.max(t[i-1][j],t[i][j-1]);
}
}
}
return t[p][q];
}
}