Given two strings str1
and str2
, return the shortest string that has both str1
and str2
as subsequences. If multiple answers exist, you may return any of them.
(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)
Example:
Input: str1 = "abac", str2 = "cab"
Output: "cabac"
Explanation:
str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.
Approach:
C++
#include <bits/stdc++.h>using namespace std;string shortestCommonSupersequence(string s1, string s2){int row = s1.size(), col = s2.size();int dp[row + 1][col + 1];dp[0][0] = 0;for (int i = 1; i <= row; i++){dp[i][0] = i;}for (int j = 1; j <= col; j++){dp[0][j] = j;}for (int i = 1; i <= row; i++){for (int j = 1; j <= col; j++){if (s1[i - 1] == s2[j - 1])dp[i][j] = 1 + dp[i - 1][j - 1];elsedp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]);}}int i = row, j = col;string str = "";while (i > 0 && j > 0){if (s1[i - 1] == s2[j - 1]){str.push_back(s1[i - 1]);i--;j--;}else{if (dp[i - 1][j] < dp[i][j - 1]){str.push_back(s1[i - 1]);i--;}else{str.push_back(s2[j - 1]);j--;}}}while (i > 0){str.push_back(s1[i - 1]);i--;}while (j > 0){str.push_back(s2[j - 1]);j--;}reverse(str.begin(), str.end());return str;}int main(){string str1 = "abac", str2 = "cab";cout << shortestCommonSupersequence(str1, str2);return 0;}
No comments:
Post a Comment