-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence
More file actions
78 lines (78 loc) · 2.2 KB
/
LongestCommonSubsequence
File metadata and controls
78 lines (78 loc) · 2.2 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package DSA;
import java.util.Scanner;
public class LongestCommonSubsequence
{
// Method to find the length of LCS using Dynamic Programming
public static int lcs(String X, String Y)
{
int m = X.length();
int n = Y.length();
int[][] dp = new int[m + 1][n + 1];
// Build LCS table in bo om-up manner
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (X.charAt(i - 1) == Y.charAt(j - 1))
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
// dp[m][n] contains the length of LCS for X[0..m-1], Y[0..n-1]
return dp[m][n];
}
// Method to print the actual LCS string
public static String getLCSString(String X, String Y)
{
int m = X.length();
int n = Y.length();
int[][] dp = new int[m + 1][n + 1];
// Fill dp table
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (X.charAt(i - 1) == Y.charAt(j - 1))
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
// Backtrack to find the LCS string
int index = dp[m][n];
char[] lcs = new char[index];
int i = m, j = n;
while (i > 0 && j > 0)
{
if (X.charAt(i - 1) == Y.charAt(j - 1))
{
lcs[index - 1] = X.charAt(i - 1);
i--;
j--;
index--;
} else if (dp[i - 1][j] > dp[i][j - 1])
{
i--;
} else {
j--;
}
}
return new String(lcs);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter first string:");
String str1 = sc.nextLine();
System.out.println("Enter second string:");
String str2 = sc.nextLine();
int length = lcs(str1, str2);
String lcsString = getLCSString(str1, str2);
System.out.println("\nLength of Longest Common Subsequence: " + length);
System.out.println("Longest Common Subsequence: " + lcsString);
sc.close();
}
}