diff --git a/1092. Shortest Common Supersequence b/1092. Shortest Common Supersequence new file mode 100644 index 0000000..5287556 --- /dev/null +++ b/1092. Shortest Common Supersequence @@ -0,0 +1,11 @@ +class Solution { +public: + string shortestCommonSupersequence(string a, string b) { + int dp[1001][1001] = {}, m = a.size(), n = b.size(); string res; + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + dp[i+1][j+1] = a[i] == b[j] ? dp[i][j] + 1 : max(dp[i][j+1], dp[i+1][j]); + 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]); + return a.substr(0, m) + b.substr(0, n) + string(rbegin(res), rend(res)); + } +};