forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.java
63 lines (58 loc) · 1.51 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//program to find longest common subsequence using dynamic programming in java
import java.util.*;
public class lcsdp
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter first string: ");//input the string1
String a=s.next(); //sample input- asdfgk
System.out.println("Enter second string: ");//input the string2
String b=s.next();//sample input- adflom
lcs(a,b);//call lcs function
//sample output-3
s.close();
}
public static void lcs(String s1, String s2)
{
int[][] storage = new int[s1.length() + 1][s2.length() + 1];
System.out.println("The length of lcs is " +lcsDP(s1, s2, storage));
}
public static int lcsDP(String s, String t, int[][] storage)
{
int m = s.length();
int n = t.length();
int storage1[][] = new int[s.length() + 1][t.length() + 1];
for(int i=0;i<=m;i++)
{ //initialize 1st row as 0
storage1[i][0]=0;
}
for(int i=0;i<=n;i++)
{//initialize 1st column as zero
storage1[0][i]=0;
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(s.charAt(m-i)==t.charAt(n-j)){//down to top approach
storage1[i][j]=1+storage1[i-1][j-1];
}
else{
storage1[i][j]=Math.max(storage1[i][j-1],storage1[i-1][j]);
//maximum out of the two
}
}
}
return storage1[m][n];
}
}
/*
Input:
Enter first string:
asdfghjk
Enter second string:
adfgiou
Output:
The length of lcs is 4
Time Complexity : O(m*n)
Space Complexity : O(m*n)
*/