Can anyone clearly explain recursive solutions of Longest common subsequence and longest common substring and the difference between them?
Asked
Active
Viewed 1,153 times
1 Answers
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.
- 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.
- 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
-
can you please give a recursive solution for substring? – vernol Sep 12 '18 at 02:14