From 489c2cd937e33e213326f889169e8bebd6bc8d6d Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 28 Feb 2025 17:04:17 +0530 Subject: [PATCH] Create 1092. Shortest Common Supersequence --- 1092. Shortest Common Supersequence | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1092. Shortest Common Supersequence 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)); + } +};