Problem
Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
- Insert
- Remove
- Replace
All of the above operations are of equal cost.
Example
1 | Input: str1 = "geek", str2 = "gesek" |
Algorithm
Method: Dynamic Programming
Problem Structure:
Define OPT(i, j) = min cost of aligning prefix string $x_1, x_2, …, x_i$ and $y_1, y_2, …y_j$
Different cases:
Case 1: OPT(i, j) matches $x_i, y_j$
Pay mismatch for $x_i$ and $y_j$ + OPT(i - 1, j - 1)
Case 2: OPT(i, j) leaves $x_i$ unmatched
Pay gap for $x_i$ + OPT(i - 1, j)
Case 3: OPT(i, j) leaves $y_j$ unmatched
Pay gap for $y_j$ + OPT(i, j - 1)
Time Complexity: O(mn), where m is the length of string x and n is the length of string y
1 | def edit_distance_dp(x, y): |
Weird Bug
The following two scripts will produce different answers.
1 | opt = [[0] * (len(y) + 1)for _ in range(len(x) + 1)] |
1 | opt = [[0] * (len(y) + 1)] * (len(x) + 1) |