leetCode 72.Edit Distance (编辑距离) 解题思路和方法

Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character

b) Delete a character

c) Replace a character

别人的思路:

自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit Distance, 也称Levenshtein distance。受到一篇Edit Distance介绍文章的启发,本文用动态规划求取了两个字符串之间的minimal Edit Distance. 动态规划方程将在下文进行讲解。 

1. what is minimal edit distance?
简单地说,就是仅通过插入(insert)、删除(delete)和替换(substitute)个操作将一个字符串s1变换到另一个字符串s2的最少步骤数。熟悉算法的同学很容易知道这是个动态规划问题。
其实一个替换操作可以相当于一个delete+一个insert,所以我们将权值定义如下:
I  (insert):1
D (delete):1
S (substitute):2

2. example:
intention->execution
Minimal edit distance:
delete i ; n->e ; t->x ; insert c ; n->u 求和得cost=8

3.calculate minimal edit distance dynamically
思路见注释,这里D[i,j]就是取s1前i个character和s2前j个character所得minimal edit distance
三个操作动态进行更新:
D(i,j)=min { D(i-1, j) +1, D(i, j-1) +1 , D(i-1, j-1) + s1[i]==s2[j] ? 0 : 2};中的三项分别对应D,I,S。(详见我同学的博客)

因为本题的替换操作权重同样为1,故字符不相等+1即可。

代码如下:

public class Solution {
    public int minDistance(String word1, String word2) {
        //边界条件
        if(word1.length() == 0)
    		return word2.length();
    	if(word2.length() == 0)
    		return word1.length();
        /*
         * 本题用动态规划的解法
         * f[i][j]表示word1的前i个单词到word2前j个单词的最短距离
         * 状态转移方程:f[i][j] =
         */

        int[][] f = new int[word1.length()][word2.length()];
        boolean isEquals = false;//是否已经有相等
        for(int i = 0 ; i < word2.length(); i++){
            //如果相等,则距离不增加
            if(word1.charAt(0) == word2.charAt(i) && !isEquals){
                f[0][i] = i > 0 ? f[0][i-1]:0;//不能从0开始
                isEquals = true;
            }else{
                f[0][i] = i > 0 ? f[0][i-1]+1:1;
            }
        }
        isEquals = false;//是否已经有相等
        for(int i = 1 ; i < word1.length(); i++){
            //如果相等,则距离不增加
            if(word1.charAt(i) == word2.charAt(0) && !isEquals){
                f[i][0] =  f[i-1][0];//不能从0开始
                isEquals = true;
            }else{
                f[i][0] = f[i-1][0]+1;
            }
        }

        for(int i = 1; i < word1.length();i++){
            for(int j = 1; j < word2.length(); j++){
                if(word1.charAt(i) == word2.charAt(j)){
                    f[i][j] = f[i-1][j-1];//相等的话直接相等
                }else{
                    f[i][j] = f[i-1][j-1]+1;
                }
                //然后与从f[i-1][j]+1,f[i][j-1]+1比较,取最小值
                f[i][j] = Math.min(f[i][j],Math.min(f[i-1][j]+1,f[i][j-1]+1));
            }
        }
        return f[word1.length()-1][word2.length()-1];
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-23 11:18:02

leetCode 72.Edit Distance (编辑距离) 解题思路和方法的相关文章

LeetCode 72. Edit Distance Java

72.Edit Distance(编辑距离) 题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路: 多次选择试图得到最优解,那么考虑动态规划. 先假设word1有len1位,word2有len2位,建立数组step,step[i][j]就代表我们要将word1前 i 位转换为word2前 j 位的最少数量. 此时word1查找到第 i+1 位字母a,

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

19.2.13 [LeetCode 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 = "h

第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三种情况考虑 分别对应三中操作 因为只需要三个值就可以更新dp[i][j] 我们可以把空间复杂度降低到O(n) Replace word1[i - 1] by word2[j - 1] (dp[i][j] = dp[i - 1][j - 1] + 1 (for replacement)); Delet

leetCode 15. 3Sum (3个数) 解题思路和方法

3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

leetCode 48.Rotate Image (旋转图像) 解题思路和方法

Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

刷题72. Edit Distance

一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一点思路也没,就直接看答案了.用的还是dp算法,dp[n1+1][n2+1]中的dp[i][j]表示将word1的前i位,变为word2的前j位需要的步骤.注意第1行是空,第1列也是空. 1.第一行中,dp[0][i]表示空字符""到word2[0,...,i]需要编辑几次 2.第一列中,d

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

72. Edit Distance &amp;&amp; 161. One Edit Distance

72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a c