72. Edit Distance
-
题目
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')Example 2:
Input: 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
-
递推关系–
核心d i s [ i ] [ j ] = m i n { d i s [ i ] [ j − 1 ] + 1 d i s [ i − 1 ] [ j ] + 1 d i s [ i − 1 ] [ j − 1 ] + ( w o r d 1 [ i − 1 ] = = w o r d 2 [ j − 1 ] ? 0 : 1 ) ) dis[i][j] = min \begin{cases} dis[i][j-1] + 1\\ dis[i-1][j] + 1\\ dis[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1))\\ \end{cases} dis[i][j]=min⎩⎪⎨⎪⎧dis[i][j−1]+1dis[i−1][j]+1dis[i−1][j−1]+(word1[i−1]==word2[j−1]?0:1))
其中三个操作的表示- 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]
- 这道题是一道动态规划的题,需要将一个字符串
-
实现代码
class Solution { public: int minDistance(string word1, string word2) { int m = word1.size(); int n = word2.size(); int dis[m+1][n+1] = {0}; for (int i = 0; i <= m; ++i) dis[i][0] = i; for (int j = 0;j <= n; ++j) dis[0][j] = j; // m or n equal 0 if (!m && !n) return max(m, n); // 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) for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { dis[i][j] = min(dis[i][j-1] + 1, min(dis[i-1][j] + 1, dis[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1))); } } return dis[m][n]; } };

本文深入解析编辑距离算法,一种用于衡量两个字符串相似度的动态规划方法。通过具体实例,阐述了如何通过插入、删除和替换操作,计算从一个字符串转换为另一个字符串所需的最小操作数。文章提供了详细的算法步骤和C++实现代码。
397

被折叠的 条评论
为什么被折叠?



