-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
64 lines (51 loc) · 1.6 KB
/
LongestCommonSubsequence.java
File metadata and controls
64 lines (51 loc) · 1.6 KB
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
64
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class LongestCommonSubsequence {
static int longestCommonSubsequence(char a[] , char b[])
{
int l1 = a.length;
int l2 = b.length;
int dp[][] = new int[l1+1][l2+1];
for(int i=1; i <= l1; i++)
{
for(int j=1; j <= l2; j++)
{
if(a[i-1] == b[j-1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = Math.max( dp[i][j-1] , dp[i-1][j]);
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
// To actually find the longestCommonSubsequence String
StringBuilder s = new StringBuilder();
for( int i=l1 , j=l2; i > 0 && j > 0; )
{
if(dp[i][j-1] == dp[i][j])
j = j-1;
else if(dp[i-1][j] == dp[i][j])
i = i-1;
else
{
s.append(a[i-1]);
i--;j--;
}
}
System.out.println("LCS is ===> " + s.reverse());
return dp[l1][l2];
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("1st String");
String a = in.next();
System.out.println("2nd String");
String b = in.next();
int ans = longestCommonSubsequence(a.toCharArray() , b.toCharArray());
System.out.println(ans);
}
}