LCS of three strings | Practice | GeeksforGeeks

Given 3 strings A, B and C, the task is to find the longest common sub-sequence in all three given sequences.
Example 1:
Input:
A = "geeks", B = "geeksfor", 
C = "geeksforgeeks"
Output: 5
Explanation: "geeks"is the longest common
subsequence with length 5.

â€‹Example 2:
Input: 
A = "abcd", B = "efgh", C = "ijkl"
Output: 0
Explanation: There's no common subsequence
in all the strings.


Your Task:
You don't need to read input or print anything. Your task is to complete the function LCSof3() which takes the strings A, B, C and their lengths n1, n2, n3 as input and returns the length of the longest common subsequence in all the 3 strings.

Expected Time Complexity: O(n1*n2*n3).
Expected Auxiliary Space: O(n1*n2*n3).

Constraints:
1<=n1, n2, n3<=20
0
amarrajsmart1973 days ago
int LCSof3 (string A, string B, string C, int n1, int n2, int n3){   // your code here   int dp[n1+1][n2+1][n3+1];   for(int i=0;i<=n1;i++)   {       for(int j=0;j<=n2;j++)       {           for(int k=0;k<=n3;k++)           {               if(i==0||j==0||k==0)               dp[i][j][k]=0;               else if(A[i-1]==B[j-1]&&A[i-1]==C[k-1])                   dp[i][j][k]=1+dp[i-1][j-1][k-1];               else               dp[i][j][k]=max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));           }       }   }   return dp[n1][n2][n3];}
0
riyanshugarg15085 days ago
int dp[n1+1][n2+1][n3+1];   for(int i=0; i<=n1; i++)   {       for(int j=0; j<=n2; j++)       {           for(int k=0; k<=n3; k++)           {               if(i==0||k==0||j==0)                   dp[i][j][k]=0;           }           }   }   for(int i=1; i<=n1; i++)   {       for(int j=1; j<=n2; j++)       {           for(int k=1; k<=n3; k++)           {               if(x[i-1]==y[j-1]&&x[i-1]==z[k-1])               {                   dp[i][j][k]= 1+dp[i-1][j-1][k-1];               }               else               {                   dp[i][j][k]= max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));               }           }           }   }   return dp[n1][n2][n3];
-1
jainmuskan5654 weeks ago
int LCSof3 (string A, string B, string C, int n1, int n2, int n3){   int dp[n1+1][n2+1][n3+1];   for(int i=0;i<=n1;i++){       for(int j=0;j<=n2;j++){           for(int k=0;k<=n3;k++){           if(i==0||j==0||k==0){               dp[i][j][k]=0;           }   }   }   }  for(int i=1;i<=n1;i++){       for(int j=1;j<=n2;j++){           for(int k=1;k<=n3;k++){               if(A[i-1]==B[j-1] && B[j-1]==C[k-1]){                   dp[i][j][k]= 1+ dp[i-1][j-1][k-1];               }               else{                   dp[i][j][k]= max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));               }           }       }  }  return dp[n1][n2][n3];}
0
cs19b01911 month ago
int LCSof3 (string A, string B, string C, int n1, int n2, int n3){   // your code here   int dp[n1+1][n2+1][n3+1];   for(int i = 0 ;i <= n1 ; i++)   {       for(int j = 0 ; j <=n2 ; j++)       {           for(int k = 0 ; k <= n3 ; k++)           {               if(i == 0 || j == 0 || k == 0)                dp[i][j][k] = 0;               else               {                   if(A[i-1] == B[j-1] && B[j-1] == C[k-1] && C[k-1] == A[i-1])                    dp[i][j][k] = 1 + dp[i-1][j-1][k-1];                   else                    dp[i][j][k] = max(dp[i][j][k-1] , max(dp[i-1][j][k] , dp[i][j-1][k]));               }           }       }   }   return dp[n1][n2][n3];}
0
whitewalter2 months ago
   int t[n1+1][n2+1][n3+1];   for(int i=0;i<=n1;i++){       for(int j=0;j<=n2;j++){           for(int k=0;k<=n3;k++){               if(i==0 || j==0 || k==0){                   t[i][j][k]=0;               }           }       }          }   for(int i=1;i<=n1;i++){       for(int j=1;j<=n2;j++){           for(int k=1;k<=n3;k++){               if((A[i-1]==B[j-1]) && (B[j-1]==C[k-1]))                   t[i][j][k]=1+t[i-1][j-1][k-1];               else                   t[i][j][k]=max({t[i-1][j][k] , t[i][j-1][k] , t[i][j][k-1]});           }       }   }   return t[n1][n2][n3];
0
annanyamathur2 months ago
int memo[100][100][100];int Lcs(string a, string b, string c, int n, int m, int x){   if(n<=0 ||m<=0|| x<=0)   return 0;   if(memo[n][m][x]==-1)   {       if(a[n-1]==b[m-1] && b[m-1]==c[x-1])       memo[n][m][x]=1+Lcs(a,b,c,n-1,m-1,x-1);       else       memo[n][m][x]=max({Lcs(a,b,c,n,m,x-1),Lcs(a,b,c,n,m-1,x),Lcs(a,b,c,n-1,m,x)});   }   return memo[n][m][x];}int LCSof3 (string a, string b, string c, int n1, int n2, int n3){   memset(memo,-1,sizeof(memo));   return Lcs(a,b,c,n1,n2,n3);   }
0
bhargav_412 months ago
Java Solution 
 
class Solution 
{ 
    int LCSof3(String A, String B, String C, int n1, int n2, int n3) 
    { 
       int[][][] dp = new int[n1+1][n2+1][n3+1];
       for(int i=0;i<n1+1;i++) for(int j=0;j<n2+1;j++) for(int k=0;k<n3+1;k++) dp[i][j][k] = -1;
       return solve(A,B,C,n1,n2,n3,dp);
        
    }
    
    public static int  solve(String A, String B, String C,int n1, int n2, int n3,int[][][] dp){
        if(n1<=0 || n2<=0 || n3<=0) return 0;
        
        if(dp[n1][n2][n3]!=-1) return dp[n1][n2][n3];
        
        if(A.charAt(n1-1)==B.charAt(n2-1) && B.charAt(n2-1)==C.charAt(n3-1))
            return dp[n1][n2][n3] = 1 + solve(A,B,C,n1-1,n2-1,n3-1,dp);
        else{
            int a = solve(A,B,C,n1-1,n2,n3,dp);
            int b = solve(A,B,C,n1-1,n2-1,n3,dp);
            int c = solve(A,B,C,n1-1,n2-1,n3-1,dp);
            int d = solve(A,B,C,n1,n2-1,n3,dp);
            int e = solve(A,B,C,n1,n2-1,n3-1,dp);
            int f = solve(A,B,C,n1,n2,n3-1,dp);
            
            int max = Math.max(a,Math.max(b,Math.max(c,Math.max(d,Math.max(e,f)))));
            return dp[n1][n2][n3] = max;
        }
    }
} 
+1
shyamprakash8072 months ago
Python solution using LCS :
 
class Solution:
   def LCSof3(self,s1,s2,s3,n1,n2,n3):       # code here       dp = [[[0 for i in range(n3+1)] for j in range(n2+1)] for k in range(n1+1)]       for i in range(1, n1+1):           for j in range(1, n2+1):               for k in range(1, n3+1):                   if s1[i-1]==s2[j-1]==s3[k-1]:                       dp[i][j][k] = 1 + dp[i-1][j-1][k-1]                   else:                       dp[i][j][k] = max(dp[i-1][j][k], dp[i][j-1][k], dp[i][j][k-1])       return dp[n1][n2][n3]
+1
lindan1232 months ago
 int lcs(int x, int y, int z, string s1, string s2, string s3)
    {
       int dp[100][100][100];
       
        for(int i=0;i<=x;i++)
        {
            for(int j=0;j<=y;j++)
            {
                for(int k=0;k<=z;k++)
                {
                    
                if( i==0 || j==0 || k==0)
                dp[i][j][k]=0;     
              
                else if ((s1[i-1] == s2[j-1]) && (s2[j-1]==s3[k-1]))       
                dp[i][j][k] = dp[i-1][j-1][k-1] + 1; 
             
                else
                dp[i][j][k] = max({dp[i-1][j][k], dp[i][j-1][k], dp[i][j][k-1]}); 
                }
            }
        }
        
        //returning the result.
        return dp[x][y][z];   
    }
int LCSof3 (string A, string B, string C, int n1, int n2, int n3)
{
    int n = A.length();
    int m = B.length();
    int k = C.length();
    
    int ans = lcs(n,m,k,A,B,C);
   
    return ans;
}
Time Taken : 0.0
Cpp
0
victor19108663 months ago
Memoization approach Easy to understand || c++
int helper(string &A,string &B,string &C,int n1,int n2,int n3,vector<vector<vector<int>>> &dp){  if(n1==0 || n2==0 || n3==0)return 0;  if(dp[n1][n2][n3]!=-1)return dp[n1][n2][n3];  if(A[n1-1]==B[n2-1] && B[n2-1]==C[n3-1]){      return dp[n1][n2][n3]=1+helper(A,B,C,n1-1,n2-1,n3-1,dp);  }  else{      return dp[n1][n2][n3]=max({helper(A,B,C,n1-1,n2,n3,dp),helper(A,B,C,n1,n2-1,n3,dp),helper(A,B,C,n1,n2,n3-1,dp)});  }}int LCSof3 (string A, string B, string C, int n1, int n2, int n3){ vector<vector<vector<int>>> dp(n1+1,vector<vector<int>>(n2+1,vector<int>(n3+1,-1))); return helper(A,B,C,n1,n2,n3,dp);}
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.