Leetcode No. 72 Edit Distance
Problem description:
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:
Insert a character
Delete a character
Replace a character
Example 1:
Input: word1 = “horse”, word2 = “ros”
Output: 3
Explanation:
horse -> rorse (replace ‘h’ with ‘r’)
rorse -> rose (remove ‘r’)
rose -> ros (remove ‘e’)
Analysis:
This problem can be solved with dynamic programming. If the length of original word and target word are l1 and l2 respectively, then we first construct a matrix dp. Each element in the matrix dp[i][j] represents a subproblem, which means the minimum number of steps it takes to convert from first i letters of word 1 to first j letters of word 2.
Subproblem relationship from bottom up:
If the i-th letter in word 1 is equal to j-th letter in word 2, then dp[i][j] equals to dp[i - 1][j - 1], because we don’t have to make any action on this letter. If the two letters are not the same, then we have to perform one action:
- delete: dp[i][j] = dp[i - 1][j] + 1
- insert: dp[i][j] = dp[i][j - 1] + 1
- replace: dp[i][i] = dp[i - 1][j - 1] + 1
Then we choose the minimum number among the three options.
The result would be dp[l2][l1].
Here’s a link of an excellent video explanation:
https://www.youtube.com/watch?v=MiqoA-yF-0M
Java Code:
1 | class EditDistance { |