-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Shortest-Common-Supersequence.cpp
88 lines (75 loc) · 2.04 KB
/
Shortest-Common-Supersequence.cpp
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
79
80
81
82
83
84
85
86
87
88
/*
*
* Runtime : 12ms, faster than 98.40%
* Memory : 10.7MB, faster than 85.57%
* prerequisite : least common subsequence in string
*/
class Solution {
public:
string shortestCommonSupersequence(string str1, string str2) {
int i = str1.length();
int j = str2.length();
int t[i+1][j+1];
// create matrix of size str1 length as row
// and str2 length as column
// base condition
for(int k = 0;k<=i;k++)
{
t[k][0] = 0;
}
// base condition
for(int k = 0;k<=j;k++)
{
t[0][k] = 0;
}
// to form different solution from all possible length string
for(int k = 1;k<=i;k++)
{
for(int l = 1;l<=j;l++)
{
if(str1[k-1] == str2[l-1])
{
t[k][l] = 1+t[k-1][l-1];
}
else{
t[k][l] = max(t[k-1][l],t[k][l-1]);
}
}
}
string res="";
while(i>0 && j>0)
{
// if char are same in str1 and str2, add it one time
if(str1[i-1]==str2[j-1])
{
res.push_back(str1[i-1]);
i--;j--;
}
else{
// comparing matrix value to form string
// add both characters of string to answer
if(t[i][j-1] > t[i-1][j])
{
res.push_back(str2[j-1]);
j--;
}
else{
res.push_back(str1[i-1]);
i--;
}
}
}
while(i>0)
{
res.push_back(str1[i-1]);
i--;
}
while(j>0)
{
res.push_back(str2[j-1]);
j--;
}
reverse(res.begin(),res.end());
return res;
}
};