Shortest Common Supersequence | Practice | GeeksforGeeks

Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.

Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100
 
0
vinamrajha1 week ago
int shortestCommonSupersequence(string X, string Y, int m, int n)
   {
       //code here
       //LCS_variation
       int dp[m+1][n+1];
       for(int i =0; i<=m; ++i)dp[i][0] = 0;
       for(int i =0; i<=n; ++i)dp[0][i] = 0;
       for(int i =1; i<=m; ++i){
           for(int j =1; j<=n; ++j){
               if(X[i-1]==Y[j-1]) dp[i][j] = 1+dp[i-1][j-1];
               else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
           }
       }
       return(m+n-dp[m][n]);
   }
0
uratabhi3 weeks ago
#include <bits/stdc++.h>
using ll = int64_t;
using iii = std::tuple<int, int, int>;
void sum(int a);
signed main()
{
    std::string X, Y;
    std::cin >> X >> Y;
    int n = X.size();
    int m = Y.size();
    std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (X[i - 1] == Y[j - 1])
            {
                dp[i][j] = 1 + dp[i - 1][j - 1];
            }
            else
            {
                dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    std::cout<<m+n-dp[n][m]<<'\n';
}
 
void sum(int a)
{
    std::cout << ++a << '\n';
}
0
hamidnourashraf1 month ago
class Solution:
    
    #find longest common subsequent and then return m+n - len(LC_subsequence)
    def shortestCommonSupersequence(self, X, Y, m, n):
        dp = [[0] * (n+1) for i in range(m+1)]
        for i in range(1, m+1):
            for j in range(1, n+1):
                if X[i-1] == Y[j-1]:
                    dp[i][j] = 1 + dp[i-1][j-1]
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
        return m+n - dp[m][n]
0
adityasingh1091 month ago
    private:
    int lcs(string s1, string s2 , int m , int n ){
         int dp[m+1][n+1];
         
         for(int i = 0; i<m+1; i++){
             for(int j = 0; j<n+1; j++){
                 if(i == 0 || j ==0)
                    dp[i][j] = 0;
             }
         }
         
         for(int i = 1; i<m+1; i++){
             for(int j = 1; j<n+1; j++){
                if(s1[i-1] ==  s2[j-1] )
                    dp[i][j] =   1+dp[i-1][j-1];
                else 
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1] );
            }
             
         }
         
         return dp[m][n];
         
         
    }
public:    
    int shortestCommonSupersequence(string X, string Y, int m, int n)
    {
        return m+n - lcs(X, Y, m , n);
    }
0
ayushkumar54511 month ago
 int dp[1001][1001];   int lcs(string &a,string &b,int n,int m){       for(int i=0;i<=n;i++){           for(int j=0;j<=m;j++){               if(i==0 or j==0) dp[i][j]=0;           }       }       for(int i=1;i<n+1;i++){           for(int j=1;j<m+1;j++){               if(a[i-1]==b[j-1]){                   dp[i][j]=1+dp[i-1][j-1];               }               else{                   dp[i][j]=max(dp[i][j-1],dp[i-1][j]);               }           }       }       return dp[n][m];   }   int shortestCommonSupersequence(string X, string Y, int m, int n)   {      int x=n-lcs(X,Y,m,n);      int y=m-lcs(X,Y,m,n);      return x+y+lcs(X,Y,m,n);   }
+1
shishankrawt937741 month ago
This is my solution with a different, but intiutive approach. 
I hope it is helpful:
 
This is the code:
// { Driver Code Starts
//Initial template for C++

#include<bits/stdc++.h>
using namespace std;


 // } Driver Code Ends
//User function template for C++


class Solution
{
    public:
    //Function to find length of shortest common supersequence of two strings.
    int shortestCommonSupersequence(string a, string b, int n1, int n2)
    {
        //code here
        int dp[n1+1][n2+1];
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 0;
        for(int i = 1; i<=n1; i++)dp[i][0] = i;  // Base Case
        for(int i = 1; i<=n2; i++)dp[0][i] = i;  // Base Case
        for(int i = 1; i<=n1; i++){
            for(int j = 1; j<=n2; j++){
                if(a[i-1] == b[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;  // If both chars are same, add only 1.
                    continue;
                }
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1; // Give the min + 1 to the ans.
            }
        }
        return dp[n1][n2];
    }
};

// { Driver Code Starts.

int main()
{   
    
    int t;
    
    //taking total testcases
    cin >> t;
    while(t--){
    string X, Y;
    //taking String X and Y
	cin >> X >> Y;
	
	//calling function shortestCommonSupersequence()
	Solution obj;
	cout << obj.shortestCommonSupersequence(X, Y, X.size(), Y.size())<< endl;
    }
	return 0;
}

  // } Driver Code Ends
0
anandkumarsatapathy1 month ago
// { Driver Code Starts//Initial template for C++
#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends//User function template for C++
class Solution{   public:   int lcs(string w1,string w2)   {       int n = w1.length();       int m = w2.length();       vector<vector<int>>dp(n+1,vector<int>(m+1,0));       for(int i =0;i<=n;i++)dp[i][0]=0;       for(int j =0;j<=m;j++)dp[0][j]=0;       for(int i = 1;i<=n;i++){           for(int j = 1;j<=m;j++){               if(w1[i-1]==w2[j-1])                   dp[i][j]=1+dp[i-1][j-1];               else                   dp[i][j]=max(dp[i-1][j],dp[i][j-1]);           }       }       return dp[n][m];   }   //Function to find length of shortest common supersequence of two strings.   int shortestCommonSupersequence(string X, string Y, int m, int n)   {       int x = n-lcs(X,Y);       int y = m-lcs(X,Y);       return x+y+lcs(X,);   }};
// { Driver Code Starts.
int main(){         int t;      //taking total testcases   cin >> t;   while(t--){   string X, Y;   //taking String X and Ycin >> X >> Y;//calling function shortestCommonSupersequence()Solution obj;cout << obj.shortestCommonSupersequence(X, Y, X.size(), Y.size())<< endl;   }return 0;}
 // } Driver Code Ends
0
amish987652 months ago
gfg ques are a cup of tea for me 
JAVA Solution:
public static int shortestCommonSupersequence(String X,String Y,int m,int n)   {       //Your code here       int[][] t=new int[m+1][n+1];       int ans=0;       for(int i=1;i<=m;i++){           for(int j=1;j<=n;j++){               if(X.charAt(i-1)==Y.charAt(j-1)){                 t[i][j]=1+t[i-1][j-1];                 }               else{                   t[i][j]=Math.max(t[i-1][j],t[i][j-1]);               }           }       }       ans=(m+n)-t[m][n];       return ans;   }
0
annanyamathur2 months ago
int dp[101][101];   int shortestCommonSupersequence(string x, string y, int n, int m)   {       for(int i=0;i<=m;i++)       dp[i][0]=0;       for(int j=0;j<=n;j++)       dp[0][j]=0;       for(int i=1;i<=n;i++)       {           for(int j=1;j<=m;j++)           {               if(x[i-1]==y[j-1])               dp[i][j]=1+dp[i-1][j-1];               else               dp[i][j]=max(dp[i-1][j],dp[i][j-1]);           }       }       int res=n+m-dp[n][m];       return res;   }
+1
madhukartemba2 months ago
JAVA SOLUTION:
class Solution
{
    
    private static int LCS(String X, String Y, int n1, int n2)
    {
        int dp[][] = new int[n1+1][n2+1];
        
        for(int i=1; i<=n1; i++)
        {
            for(int j=1; j<=n2; j++)
            {
                if(X.charAt(i-1)==Y.charAt(j-1))
                {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else
                {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        
        return dp[n1][n2];
        
        
    }
    
    //Function to find length of shortest common supersequence of two strings.
    public static int shortestCommonSupersequence(String X,String Y,int m,int n)
    {
        
        int lcs = LCS(X, Y, m, n);
        
        return lcs + (m-lcs) + (n-lcs);
        
        
        
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.