Solution Review: Longest Common Substring
Understand how to solve the longest common substring problem using various approaches including simple recursion, top-down memoization, and bottom-up dynamic programming with tabulation. Learn to optimize space complexity while maintaining time efficiency using these methods.
We'll cover the following...
We'll cover the following...
Solution 1: Simple recursion
Explanation
Let’s look at this problem on a smaller scale. We are comparing each character one by one with the other string. There can be three possibilities for i character of str1 and j character of str2. If both characters match, these could be part of a common substring meaning we should count this length (lines 6-7). In the case that these characters do not match, we could have two further possibilities:
icharacter might match with(j+1)character.jcharacter might match with(i+1)