题目
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:1
2
3
4
5
6Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:1
2
3
4
5
6
7
8Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
思路解析
- 这道题是一道动态规划的题,需要将一个字符串
word1经过add、delete、replace三种操作变换成另一个字符串word2,求最小的编辑距离. 解题思路:假设
word1=[123...n],word2=[123...m],要求使得word1转换成word2。对于这个问题,我们将其分解为子问题求解。- 定义dis[i][j]
- 表示
word1' = [1..i]转换成word2' = [1...j]的编辑距离(i代表word1前i个字符,j代表word2前j个字符) - 因此
word1到word2的编辑距离为dis[n][m]
- 表示
- 求解
word1到word2的编辑距离,我们可以求取word1的前i个字符(0 < i < n)到word2的前j个字符(0 < j < m)的编辑距离dis[i][j]。当然每个dis[i][j]都基于之前的计算。 步骤
- 初始化
- dis[i, 0] = i
- dis[0, j] = j
递推关系—
核心其中三个操作的表示
- insert:
dis[i, j] = dis[i][j-1] + 1 - delete:
dis[i, j] = dis[i-1][j] + 1 replace or no op:
dis[i, j] = dis[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1)对于每
dis[i][j],我们选取最小编辑距离
- insert:
- 初始化
- 最后得到的
dis[n][m]就是word1到word2的编辑距离
- 定义dis[i][j]
实现代码
1 | class Solution { |