We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 0d44d42 + 489c2cd commit 278634eCopy full SHA for 278634e
1092. Shortest Common Supersequence
@@ -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