-3

Can anyone clearly explain recursive solutions of Longest common subsequence and longest common substring and the difference between them?

vernol
  • 15
  • 8

1 Answers1

0

Longest common subsequence is a classical problem generally solved by dynamic programming technique.

The recursive relation for strings S, T is:

/*Characters do not match*/
if(S[i]!=T[j])
   return max(LCS(i+1, j, S, T), LCS(i, j+1, S, T));
else 
   return LCS(i+1, j+1, S, T) + 1;

In this, i represents index of String S, and j of string T. At any state (i, j), there are possible scenarios, either the characters match or they do not match.

  1. If the characters do not match then, we have two possible options. Either choose the next char of S or choose next char of T, and take the best of both the solutions.
  2. If the characters match, then iterate to next character of both the strings.

Similarly this can be solve for substring

Shubham Agrawal
  • 298
  • 4
  • 10