Skip to content

Commit 3dc9743

Browse files
authored
Merge pull request #730 from AmanPandey0198/patch-2
Finding the Shortest Common Supersequence.java
2 parents ce58a90 + 6d7dcd2 commit 3dc9743

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class Main
2+
{
3+
// Function to return an SCS of substrings `X[0…m-1]`, `Y[0…n-1]`
4+
public static String SCS(String X, String Y, int m, int n, int[][] lookup)
5+
{
6+
// if the end of the first string is reached,
7+
// return the second string
8+
if (m == 0) {
9+
return Y.substring(0, n);
10+
}
11+
12+
// if the end of the second string is reached,
13+
// return the first string
14+
if (n == 0) {
15+
return X.substring(0, m);
16+
}
17+
18+
// if the last character of `X` and `Y` matches, include it in SSC
19+
// and recur to find SCS of substring `X[0…m-2]` and `Y[0…n-1]`
20+
if (X.charAt(m - 1) == Y.charAt(n - 1)) {
21+
return SCS(X, Y, m - 1, n - 1, lookup) + X.charAt(m - 1);
22+
}
23+
// if the last character of `X` and `Y` don't match
24+
else {
25+
26+
// if the top cell of a current cell has less value than the left
27+
// cell, then include the current character of string `X` in SCS
28+
// and find SCS of substring `X[0…m-2]` and `Y[0…n-2]`
29+
30+
if (lookup[m - 1][n] < lookup[m][n - 1]) {
31+
return SCS(X, Y, m - 1, n, lookup) + X.charAt(m - 1);
32+
}
33+
34+
// if the left cell of a current cell has less value than the top
35+
// cell, then include the current character of string `Y` in SCS
36+
// and find SCS of substring `X[0…m-1]` and `Y[0…n-2]`
37+
else {
38+
return SCS(X, Y, m, n - 1, lookup) + Y.charAt(n - 1);
39+
}
40+
}
41+
}
42+
43+
// Function to fill the lookup table by finding the length of SCS of
44+
// sequences `X[0…m-1]` and `Y[0…n-1]`
45+
public static void SCSLength(String X, String Y, int m, int n, int[][] lookup)
46+
{
47+
// initialize the first column of the lookup table
48+
for (int i = 0; i <= m; i++) {
49+
lookup[i][0] = i;
50+
}
51+
52+
// initialize the first row of the lookup table
53+
for (int j = 0; j <= n; j++) {
54+
lookup[0][j] = j;
55+
}
56+
57+
// fill the lookup table in a bottom-up manner
58+
for (int i = 1; i <= m; i++)
59+
{
60+
for (int j = 1; j <= n; j++)
61+
{
62+
// if the current character of `X` and `Y` matches
63+
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
64+
lookup[i][j] = lookup[i - 1][j - 1] + 1;
65+
}
66+
// otherwise, if the current character of `X` and `Y` don't match
67+
else {
68+
lookup[i][j] = Integer.min(lookup[i - 1][j] + 1, lookup[i][j - 1] + 1);
69+
}
70+
}
71+
}
72+
}
73+
74+
public static void main(String[] args)
75+
{
76+
String X = "ABCBDAB", Y = "BDCABA";
77+
78+
int m = X.length(), n = Y.length();
79+
80+
// lookup table stores solution to already computed subproblems
81+
// lookup[i][j] stores the length of SCS of substring `X[0…i-1]` and `Y[0…j-1]`
82+
int[][] lookup = new int[m + 1][n + 1];
83+
84+
// fill the lookup table in a bottom-up manner
85+
SCSLength(X, Y, m, n, lookup);
86+
87+
// find the shortest common supersequence by reading the lookup
88+
// table in a top-down manner
89+
System.out.print("The shortest common supersequence of " + X +
90+
" and " + Y + " is " + SCS(X, Y, m, n, lookup));
91+
}
92+
}

0 commit comments

Comments
 (0)