Before even getting to the DP versions of the solution, when I see the recursive solutions, the main difference are in the following 2 lines:
LC Substring:
if(s1.charAt(i1) == s2.charAt(i2))
count = findLCSLengthRecursive(s1, s2, i1+1, i2+1, count+1);
LC Subsequence:
if(s1.charAt(i1) == s2.charAt(i2))
return 1 + findLCSLengthRecursive(s1, s2, i1+1, i2+1);
Of course for substring, we return the cummulative count when we reach the leaf. The subsequence returns the count (+1) as we go up the tree.
I can understand the subsequence algorithm. I am not able to understand how the substring algorithm above makes sure that we are getting a continuous “sub-string” of characters. Can someone please help with explaining that in simple terms? And I would request the author of this course to consider adding a line or 2 about this confusion in the algorithm description if you think it makes sense.
Additional question: How does adding a “1+” before calling the LCS function vs doing a “count+1” and passing it as a parameter into the function make all the difference?
thanks
-V