Solution: Strings Interleaving
Let's analyze the different ways to solve the string interleaving problem.
We'll cover the following...
We'll cover the following...
Solution 1: brute force
Explanation
The problem follows the longest common subsequence pattern.
A basic brute force solution is to try matching m and n with p one letter at a time. Let’s assume m_index, n_index, and p_index represent the current indexes of m, n, and p strings respectively. Therefore, we have two options at any step:
- If the letter at ‘m_index’ matches the letter at ‘p_index’, we can recursively match for the remaining lengths of ‘m’ and ‘p’
- If the letter at ‘n_index’ matches the letter at ‘p_index’, we can recursively match for the remaining lengths of ‘n’ and ‘p’
Time complexity
The time complexity of the above algorithm is exponential ...