LeetCode Top 100 Liked Questions 72. Edit Distance (Java版; Hard)
题目描述
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')
class Solution {
public int minDistance ( String word1, String word2) {
int n = word1. length ( ) , m = word2. length ( ) ;
int [ ] [ ] dp = new int [ n+ 1 ] [ m+ 1 ] ;
for ( int i= 1 ; i<= n; i++ ) {
dp[ i] [ 0 ] = dp[ i- 1 ] [ 0 ] + 1 ;
}
for ( int j= 1 ; j<= m; j++ ) {
dp[ 0 ] [ j] = dp[ 0 ] [ j- 1 ] + 1 ;
}
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= m; j++ ) {
if ( word1. charAt ( i- 1 ) == word2. charAt ( j- 1 ) ) {
dp[ i] [ j] = dp[ i- 1 ] [ j- 1 ] ;
} else {
dp[ i] [ j] = 1 + Math. min ( dp[ i- 1 ] [ j] , Math. min ( dp[ i] [ j- 1 ] , dp[ i- 1 ] [ j- 1 ] ) ) ;
}
}
}
return dp[ n] [ m] ;
}
}
第一次做, 牢记编辑距离的定义; 使用自底向上的动态规划; 细节:dp数组中的索引表示子串的长度, 所以dp数组中的索引转换成字符串中的索引时要减一!
先明确什么是编辑距离: 编辑距离指的是在两个单词<w_1,w_2>之间,由其中一个单词w_1转换为另一个单词w_2所需要的最少单字符编辑操作次数。
联想跳台阶那道题, 分析动态规划的递推式时, 只考虑一次操作! 只考虑一次操作!只考虑一次操作! 一次操作有几种选择? 本题是三种: 插入(insert), 删除(delete), 替换(replace).
以horse和ros为例,
(1)对什么单词进行一次插入操作, 可以求出horse转换成ros的编辑距离? 当我们已知horsr转换成ro的编辑距离时, 此时在horse后面插入一个s, 就能得到horse转换成ros的编辑距离
(2)对什么单词进行一次删除操作, 可以求出horse转换成ros的编辑距离? 当我们已知hors转换成ros的编辑距离时, 此时删除horse最后的e, 就能得到horse转换成ros的编辑距离
(3)对什么单词进行一次替换操作, 可以求出horse转换成ros的编辑距离? 当我们已知hors转换成ro的编辑距离时, 此时将horse最后的e替换成s, 就能得到horse转换成ros的编辑距离
特殊情况:
(1)当word1.charAt(i) == word2.charAt(j)时, 此状态的编辑距离等于word1[0,...,i-1]与word1[0,...,j-1]的编辑距离
(2)当word1是空字符串时, 我们只需要每次向word1中添加一个word2中的字符就能得到word2, 此时的编辑距离等于word2.length()
(3)当word2是空字符串时, 我们只需要每次删除word1中的一个字符就能得到word2, 此时的编辑距离等于word1.length()
变量说明:
m = word1.length();
n = word2.length();
int[][] dp = new int[m+1][n+1];
dp中的每个元素dp[i][j]表示word1的前i个字符和word2的前j个字符的编辑距离, i==0或者j==0对应特殊情况,先处理两种特殊情况;
我们最终需要word1的前m个字符和word2的前n个字符的编辑距离,也就是dp[m][n];
注意细节:处理dp[i][j]时, 根据word1的第i个字符是否等于word2的第j个字符分成两种情况进行讨论,
word1的第i个字符对应为word1.charAt(i-1); word2的第j个字符对应为word2.charAt(j-1)
递推公式:
根据word1的第i个字符是否等于word2的第j个字符分成两种情况进行讨论
当word1.charAt(i-1)==word2.charAt(j-1)时, dp[i][j] = dp[i-1][j-i]
当word1.charAt(i-1)!=word2.charAt(j-1)时, dp[i][j] = 1 + min(dp[i][j-1] + dp[i-1][j] + dp[i-1][j-1])
其中,
dp[i][j-1]到dp[i][j]对应插入操作, 如已知horse转成ro的编辑距离, 此时在horse末尾插入一个s就得到horse到ros的编辑距离
dp[i-1][j]到dp[i][[j]对应删除操作, 如已知hors转成ros的编辑距离, 此时删除horse末尾的e就得到horse到ros的编辑距离
dp[i-1][j-1]到dp[i][j]对应替换操作, 如已知hors转成ro的编辑距离, 此时将horse末尾的e替换成s就得到horse到ros的编辑距离
class Solution {
public int minDistance ( String word1, String word2) {
int m = word1. length ( ) , n = word2. length ( ) ;
int [ ] [ ] dp = new int [ m+ 1 ] [ n+ 1 ] ;
for ( int i= 0 ; i<= m; i++ )
dp[ i] [ 0 ] = i;
for ( int j= 0 ; j<= n; j++ )
dp[ 0 ] [ j] = j;
for ( int i= 1 ; i<= m; i++ ) {
for ( int j= 1 ; j<= n; j++ ) {
if ( word1. charAt ( i- 1 ) == word2. charAt ( j- 1 ) ) {
dp[ i] [ j] = dp[ i- 1 ] [ j- 1 ] ;
}
else {
dp[ i] [ j] = Math. min ( dp[ i- 1 ] [ j] + 1 , dp[ i] [ j- 1 ] + 1 ) ;
dp[ i] [ j] = Math. min ( dp[ i] [ j] , dp[ i- 1 ] [ j- 1 ] + 1 ) ;
}
}
}
return dp[ m] [ n] ;
}
}
力扣上非常棒的题解
题解链接
思路过程可以如下:
需要求解的是word1编辑到word2所需要的步骤最小值,是不是可以先把word1的一个子串编辑成word2的一个子串?然后随着子串的长度逐渐变大,是否可以推导出结果?
用W1Si表示word1的子串(sub(0, i)),W2Sj表示word2的子串(sub(0, j));
二维数组dp[i][j] 代表把 W1Si编辑成 W2Sj所需要的最少步数;
假如现在word1为horse,word2为ros;
把horse转化为ros,可以转换思路,可在已知以下三种情况之下,再做一个额外的操作,实现把horse编辑为ros:
1、当前已经有hors编辑为ros的步骤,那么可以在原word1(horse)基础之上,删除最后的e,也可以得到ros;
2、当前已经有horse编辑成ro的步骤,那么可以在原word1(horse)基础之上,插入一个s,也可以得到ros;
3、当前已经有了hors编辑成ro的步骤,那么可以在原word1(horse)基础之上,把最后的一个e替换成s,也可以得到ros;
特殊情况下:
hors->ros,其实就等于hor->ro;
#状态转移方程
那么就可以认为上面三种情况下最小值,就是最终结果:
dp(horse->ros) = min{dp(hors->ros), dp(horse->ro), dp(hors->ro)} + 1;
即:dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1;
特殊情况下:
如果word1[i]==word2[j],那么dp[i][j]=dp[i-1][j-1];
#边界
现在来看边界:
边界是i=0或者j=0;
如果i=0,表示从horse的一个空子串(“”)编辑成ros的所有子串(""、“r”、“ro”、“ros”)所需要的步数,每一个都执行插入就可以了,结果为dp[0][j] = j;
如果j=0,表示从horse的所有子串(“”、“h”、“ho”、“hor”、"hors"、“horse”)编辑成ros的一个空子串(“”)所需要的步数,每一步都执行删除就可以了,结果为dp[i][0]=i;