cant find where my code is wrong
link:-
Lcs of 3 strings
Try using the bottom up approach. Its easier and simpler to understand.
Your corrected code using top down approach
why the condition is seperately used???
and one more thing that is this syntax wrong return dp[i][j]=max(a,b); or its necessary to first assign and then return
Hello @riprogerdep,
-
He has only added a conditional check:
if(n==0||m==0||k==0)
{
dp[n][m][k] = 0;
return dp[n][m][k];}
This implies that if the length of any of the three is 0 then the common length would also be zero.
Because you are not considering any character from one of the string.
Reason of writing this separately:
As in the next if statement he comparing elements at n-1, k-1,m-1.
So, if any of the n,m and k is 0, the index will become -1 which is not valid.
Hence, you can remove the following statements as m, n, and k will never become negative:
if(n<0||m<0||k<0)
return 0;
https://ide.codingblocks.com/s/186425 -
No, the syntax is not wrong.
As we know that the associativity of “=” i.e. assignment operator if from right to left.
So, on executing return dp[i][j]=max(a,b);
first, the max of a and b will be assigned to dp[i][j] then the value stored at dp[i][j] will be returned.
Hope, this would help.
Give a like if you are satisfied.
thank you bhai for your very good explanation:)