Solution: The Edit Distance Problem
Explore different dynamic programming methods to solve the Edit Distance problem. Learn how brute force, memoization, and tabularization approaches work and how to analyze their time complexities. This lesson enhances your ability to optimize string transformation problems in coding interviews.
Solution #1: Brute Force
We process all the characters of each string one by one. There are two possibilities for every pair of characters being considered,
Pseudocode
IF str1[m-1]==str2[n-1]
Ignore them and get the count for remaining strings. So we call the function for lengths m-1 and n-1.
ELSE
we consider all operations on ‘str1’, consider all three operations on the last character of the first string, recursively compute the minimum cost for all three operations and return the minimum of the three values.
Insert: Call function for m and n-1
Remove: Call function for m-1 and n
Replace: Call function for m-1 and n-1
Time Complexity
The time complexity of this code is exponential, i.e. ...