Skip to content
本页目录

0072-编辑距离

https://leetcode.cn/problems/edit-distance

给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

  • 插入一个字符
  • 删除一个字符
  • 替换一个字符

示例 1:

输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')

示例 2:

输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')

提示:

0 <= word1.length, word2.length <= 500
word1 和 word2 由小写英文字母组成

基本思路 【动态规划 ME】

定义 dp 数组 dp[i,j] = 表示 text[0:i] 和 text[0:j] 的编辑距离 如果 text1[i-1] == text2[j-1] , dp[i,j] = dp[i-1,j-1] 如果 text1[i-1] != text2[j-1] , dp[i,j] = dp[i-1,j-1]+1

初始化,当 i=0, 或者 j=0 的时候, 由0个字符变成1个字符的编辑距离是1,所以初始化为1,当 i=0 & j = 0的时候, dp[0,0] = 0;

终于根据自己的思路做出了这个动态规划题目
其中一定要明白 dp 数组表示了什么, i 表示什么 j 表示什么
下一步就是边界的定义 初始化 dp[0,0] = 0, dp[i,0] 表示0个字符串到目标字符串的距离,应该是等于 i 而不是等于1
然后是 状态转移方程, dp[i,j] 从上面一步如何转化而来 min 包含的分别是 替换字符,删减字符和增加字符的方式,哪种小就用哪种
感觉有点心得了

参考代码

csharp
public class Solution {
    public int MinDistance(string word1, string word2) {
    	int m = word1.Length;
    	int n = word2.Length;
    	int[,] dp = new int[m+1,n+1];
    	//初始化
    	dp[0,0] = 0;
    	for(int i=1; i<=m; i++){
    		dp[i,0] = i;
    	}
    	for(int j=1; j<=n; j++){
    		dp[0,j] = j;
    	}
    	//状态转移
    	for(int i=1; i<=m; i++){
    		for(int j=1; j<=n; j++){
    			if(word1[i-1] == word2[j-1]){
    				dp[i,j] = dp[i-1,j-1];
    			}
    			else{
    				dp[i,j] = Math.Min(Math.Min(dp[i,j-1],dp[i-1,j]),dp[i-1,j-1])+1;
    			}
    		}
    	}
        // 打印dp表格
        // for(int i=0;i<=m;i++){
        //     for(int j=0;j<=n;j++){
        //         Console.Write("{0}",dp[i,j]);
        //     }
        //     Console.WriteLine();
        // }

    	return dp[m,n];
    }
}

参考代码:复习2022-04-03

csharp
public class Solution {
    public int MinDistance(string word1, string word2) {
        int m = word1.Length;
        int n = word2.Length;
        //dp[i,j]表示 word1 前i个字符变成 word2 的前j个字符需要的最小操作数
        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[i-1] == word2[j-1]){
                    dp[i,j] = dp[i-1,j-1];
                }
                else{
                    //从左上3中状态转移过来
                    dp[i,j] = Math.Min(Math.Min(dp[i-1,j],dp[i,j-1]),dp[i-1,j-1]) + 1;
                }
            }
        }
        return dp[m,n];
    }
}

复习:20220515

注意不相等的时候3中转移方法

csharp
public class Solution {
    public int MinDistance(string word1, string word2) {
        int m = word1.Length; 
        int n = word2.Length;
        int[,] dp = new int[m+1,n+1];
        //初始化
        for(int i=1; i<=m; i++){
            dp[i,0] = i;
        }
        for(int j=1; j<=n; j++){
            dp[0,j] = j;
        }
        //状态转移
        for(int i=1; i<=m; i++){
            for(int j=1; j<=n; j++){
                if(word1[i-1] == word2[j-1]){
                    dp[i,j] = dp[i-1,j-1];
                }
                else{
                    //不相等可以由3中状态转移过来
                    // 1. 最后两个字符替换
                    // 2. 删除 word1 中一个字符
                    // 3. 删除 word2 中一个字符
                    dp[i,j] = Math.Min(Math.Min(dp[i-1,j-1],dp[i,j-1]),dp[i-1,j]) + 1;
                }
            }
        }
        return dp[m,n];
    }
}

Released under the MIT License.