Skip to content

Commit 278634e

Browse files
authored
Create 1092. Shortest Common Supersequence (#727)
2 parents 0d44d42 + 489c2cd commit 278634e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

1092. Shortest Common Supersequence

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string shortestCommonSupersequence(string a, string b) {
4+
int dp[1001][1001] = {}, m = a.size(), n = b.size(); string res;
5+
for (int i = 0; i < m; i++)
6+
for (int j = 0; j < n; j++)
7+
dp[i+1][j+1] = a[i] == b[j] ? dp[i][j] + 1 : max(dp[i][j+1], dp[i+1][j]);
8+
while (m && n) res += dp[m][n] == dp[m-1][n] ? a[--m] : dp[m][n] == dp[m][n-1] ? b[--n] : min(a[--m], b[--n]);
9+
return a.substr(0, m) + b.substr(0, n) + string(rbegin(res), rend(res));
10+
}
11+
};

0 commit comments

Comments
 (0)