# Find the longest subsequence of two strings

I want to know which is the best way to find the longest common subsequence of two strings

• What method did you try to use, and why do you think it's wrong? – Yuval Filmus May 10 '13 at 2:53
• @YuvalFilmus I used dynamic programming to find a string that is not a subsequence of A and another string that is not a subsequence of B and try to get something out of it. But my approach is wrong – user8105 May 10 '13 at 3:23
• Just a clarification, do you want a subsequence, or substring? – Luke Mathieson May 10 '13 at 4:36
• Wait! Your last edit totally changed the problem/question you are asking about. Don't do that. – D.W. May 10 '13 at 16:00

You know the recipe for dynamic programming: define some subproblems, then show how to use solutions to smaller subproblems to help you solve larger subproblems. So, what you have tried as candidate subproblems?

Hint: When you have a dynamic programming problem that takes a string as input, what are some candidate ways to define subproblems? (Prefixes, suffices, substrings...) Now, how might you apply that general advice here?

I'm not going to spell out the full answer, as this sounds like homework, and to learn from homework, you need to struggle with it yourself.

• Here OP talks about subsequences not substring, and seems he knows basics of DP. – user742 May 10 '13 at 9:26
• @SaeedAmiri I tried to remove longest common subsequence in both and try out with the rest. But it was not the right approach – user8105 May 10 '13 at 10:59
• @D.W. I know what dynamic programming is but I am not able think of the subproblem that is right for me – user8105 May 10 '13 at 11:03
• @user8105 Don't expect us to solve the assignment for you. Continue thinking, and eventually you'll find the solution. – Yuval Filmus May 10 '13 at 13:53
• @YuvalFilmus I have solved the problem – user8105 May 10 '13 at 14:55