What is the error in my code

try to run this input
ADGH
AEDH
Correct Ans : ADH
Your ans : AH

this approach is not correct
you can verify this by printing ans in each iteration
and also print the dp array in main()

Correct Approach

Following is a detailed algorithm to print the LCS. It uses the same DP table that was calculated using count LCS program.

  1. Construct dp[m+1][n+1] using the count LCS dynamic programming solution.

  2. The value dp[m][n] contains length of LCS. Create a character array lcs[] of length equal to the length of lcs plus 1 (one extra to store \0).

  3. Traverse the 2D array starting from dp[m][n]. Do following for every cell dp[i][j]

If characters (in X and Y) corresponding to dp[i][j] are same (Or X[i-1] == Y[j-1]), then include this character as part of LCS.

Else compare values of dp[i-1][j] and dp[i][j-1] and go in direction of greater value.

it gives correct ans in rec what i did wrong in memozation plz check my code

recursion calls are correct
but the way you store the ans in dp is wrong
what does dp[i][j] represent in you memoization ??

please provide me the top down approach of this

Top Down Approach

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.