Print LCS problem


What’s the mistake in my code as 1 test case is giving WA?

@nayakashutosh99 this is incorrect way to print LCS as you may intermix various multiple LCS.
hence start travel from i=0,j=0 at dp[i][j] and check if(a[i]==b[j]),then add this character to your answer and also increment i&j.
else
move towards right or down depending upon higher value for dp[i+1][j] or dp[i][j+1]

But I did the same thing. Just stored the characters in order in a hashmap wherever I got equal characters in both the strings

try this test for your code:
abcefgd
acbdefg

this will guarantee that you print one of the possible LCS only

It’s giving wrong answer


see this!

Got it. Thank you very much!!!