Solution: The Edit Distance Problem
This review provides a detailed analysis of the ways to solve the famous edit distance problem.
Solution 1: brute force
Explanation
We process all the characters of each string, one-by-one. There are two possibilities for every pair of characters being considered:
-
The end characters of both the strings match, in which case, the lengths of both the strings are reduced by one and the function is called recursively on the remaining strings
-
The end characters of both the strings do not match, in which case, all three operations (insertion, deletion, and substitution) are carried out on the last character of the first string
-
The minimum cost for all three operations is computed recursively and returned
Time complexity
The time complexity of this code is exponential, i.e. ...