Impostor_Syndrome's blog

By Impostor_Syndrome, history, 3 years ago, In English

These are the approaches that i have tried but getting TLE

Use a 3d array and continue just like we do LCS of two strings.

Complexity = O(n^3).

First take the first two strings, find the length of LCS, then recover all the LCS of these two strings using backtracking on the DP array of these two strings.

Then use these recovered LCS and find the LCS length of these strings with the last string that was given to us, and take the max len of these LCS's as the answer. Complexity = O(n^2) + complexity of recovering all the LCS strings of the first two strings

This is the link to the question ---> https://www.codingninjas.com/codestudio/problems/lcs-of-3-strings_842499?topList=top-fintech-software-engineer-interview-questions&leftPanelTab=0

Can anyone suggest a more efficient method to do this question.
--- This is the code for second method. ``` VVI lcs(string a, string b){ int n1=a.length(), n2 = b.length();

VVI dp(n1+1, VI (n2+1, 0));
for(int i=1;i<=n1;i++){
    for(int j=1;j<=n2;j++){
       if(a[i-1] == b[j-1]){
         dp[i][j] = 1 + dp[i-1][j-1];
       }
       dp[i][j] = max(dp[i-1][j], max(dp[i][j], dp[i][j-1]));
       //cout<<dp[i][j]<<" ";
    }
    //cout<<endl;

}
return dp;

}

string recoverLCS(string a, string b, VVI &dp, int i, int j){ //int n1 = a.length(), n2 = b.length();

if(i == 0 || j == 0) return "";

if(a[i-1] == b[j-1]){
    return a[i-1] + recoverLCS(a, b, dp, i-1, j-1);
}
if(dp[i-1][j] > dp[i][j-1]) return recoverLCS(a, b, dp, i-1, j);
return recoverLCS(a, b, dp, i, j-1);

}

vector recoverAllLCS(string a, string b, VVI &dp, int i, int j){ if(i == 0 || j == 0) return {""};

if(a[i-1] == b[j-1]){
    vector<string> temp = recoverAllLCS(a, b, dp, i-1, j-1);

    for(int z=0;z<(int)temp.size();z++){
       temp[z] = a[i-1] + temp[z];
    }
    // for(auto x: temp){
    //     x = a[i-1]  + x;     
    // }

    return temp;
}
else if(dp[i-1][j] > dp[i][j-1]){
    return recoverAllLCS(a, b, dp, i-1, j);
}
else if(dp[i-1][j] < dp[i][j-1]){
    return recoverAllLCS(a, b, dp, i, j-1);
}
else{
    vector<string> vs1 = recoverAllLCS(a, b, dp, i-1, j);
    vector<string> vs2 = recoverAllLCS(a, b, dp, i, j-1);
    for(auto x: vs1){
       vs2.push_back(x);
    }
    return vs2;
}

} vector returnAllLCS(string a, string b){ VVI dp = lcs(a, b); int n1=a.length(), n2 = b.length();

vector<string> v = recoverAllLCS(a, b, dp, n1, n2);
unordered_set<string> ans(v.begin(), v.end());
// for(auto x: ans){
//  reverse(x.begin(), x.end());
//  cout<<x<<endl;
// }
vector<string> final(ans.begin(), ans.end());
return final;

} ```

This is the code for first method.

VVVI dp(n1+1, VVI (n2+1, VI (n3+1, 0)));
	int ans=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] == b[j] && b[j] == c[k]){
					dp[i][j][k] = 1 + dp[i-1][j-1][k-1];
				}
				

				vector<int> di = {0, 0, -1};
				vector<int> dj = {0, -1, 0};
				vector<int> dk = {-1, 0, 0};

				for(int z=0;z<(int)di.size();z++){
					dp[i][j][k] = max(dp[i][j][k], dp[i+di[z]][j+dj[z]][k+dk[z]]);
				} 

				ans=max(ans, dp[i][j][k]);
			}
		}
	}

`

| Write comment?
»
3 years ago, # |
  Vote: I like it +2 Vote: I do not like it

i used same code to get ac

int LCS(string X, string Y, string Z, int m, int n, int o) { int L[m+1][n+1][o+1]; for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { for (int k=0; k<=o; k++) { if (i == 0 || j == 0||k==0) L[i][j][k] = 0;

else if (X[i-1] == Y[j-1] && X[i-1]==Z[k-1])
                L[i][j][k] = L[i-1][j-1][k-1] + 1;

            else
                L[i][j][k] = max(max(L[i-1][j][k],
                                     L[i][j-1][k]),
                                 L[i][j][k-1]);
        }
    }
}
return L[m][n][o];

}

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Really when? When I submitted I got 5/8 correct for O(n^3) solution.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      You can try submit this code

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it +2 Vote: I do not like it

        Figured it out. Each time i'm creating a 3 vectors, and these vectors are created and destroyed O(n^3) times, so it was taking more time than expected. I removed that and it got accepted. This is the first time that i got this kind of error.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 3   Vote: I like it +22 Vote: I do not like it

      The problem might be the time it takes to initialise the vector, for an array a NxKxJ dimesioned array takes the same time as NxJxK or any permutation of the three takes the same amount of time. but in the case of a vector number of constructor call and overhead changes according to how the whole vector in declared (PS. it'll take lowest time for NxJxK vector where N<J<K).

      Over this vectors anyhow take a little more time to get declared and initialised.

      Hence, this might be the reason that you solution TLE-d for the max test cases and her solution passed