Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)


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

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

  1. 插入一个字符
  2. 删除一个字符
  3. 替换一个字符

示例 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')

我们定义dp[i][j]为:
截取长度为i的word1字符串 需要更改多少次才能变成 截取长度为j的字符串。
举例:
示例1中,dp[1][1]代表"h"->"r" 需要多少次?
dp[2][2]表示"ho"变成"ro"需要多少次? 我们可以写出状态转移方程:如果word1[i]==word2[j],那么dp[i][j] = dp[i-1][j-1];
如果不相等,那么dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]); 我们画一下dp的表格

我们分析第一行内容:

h变成r需要1次,h->ro需要2次,h->ros需要3次。

那么这三个空填入的值分别为:1、2、3

h->ro是由h->r然后再增加一个o变成的,依次类推。

我们再来看这一步,ho->r需要2步,ho->ro,其中第二个o是相同的,不需要更改,所以等同于h->r

于是,状态转移方程就分析完了:

如果word1[i]==word2[j],那么dp[i][j] = dp[i-1][j-1];
如果不相等,那么dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]);
AC代码:
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+1; i++) {
dp[i][0] = dp[i-1][0]+1;
}
for (int i = 1; i < n+1; i++) {
dp[0][i] = dp[0][i-1]+1;
}
for (int i = 1; i < m+1; i++) {
for (int j = 1; j < n+1; j++) {
if(word1.charAt(i-1)==word2.charAt(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;
}
}
}
return dp[m][n];
}
}

Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)的更多相关文章

  1. [Leetcode 72]编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  2. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  3. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  4. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  5. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

  6. 行编辑距离Edit Distance——动态规划

    题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作:  1. 在给定位置上插入一个字符  2. 替换随意字符  3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...

  7. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  8. 编辑距离Edit Distance 非常典型的DP类型题目

    https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.c ...

  9. 动态规划dp专题练习

    貌似开坑还挺好玩的...开一个来玩玩=v=... 正好自己dp不是很熟悉,就开个坑来练练吧...先练个50题?小目标... 好像有点多啊QAQ 既然是开坑,之前写的都不要了! 50/50 1.洛谷P3 ...

随机推荐

  1. PHP类知识----析构方法点滴

    网页执行完毕 手动调用析构方法 通过unset() 应用场景:断开mysql的连接 不同情况下,析构方法调用的时机不同,程序执行顺序如下 <?php class mycoach { public ...

  2. IO—转换流和键盘录入

    简单来说,由于方法的局限性和功能的需要,特此产生了转换流. InputStreamReader是字节流转换字符流的桥梁,为了提高效率,可以在缓冲区中放入转化流的对象,,并且构造函数第二个参数可以传入一 ...

  3. hdu 6076 Security Check

    题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6076 2017 Multi-University Training Contest - Team 4 ...

  4. 【C#-程序时间计数器】如何计算某些步骤/过程耗时多少?

    使用Stopwatch对象,TimeSpan对象 Stopwatch sw = new Stopwatch();//跑表,该类可以进行时间的统计 命名空间using System.Diagnostic ...

  5. Module parse failed: Export 'instance' is not defined (35:19)

    Module parse failed: Export 'instance' is not defined (35:19) 使用npm出现的这错误,用yarn就可以了 这种情况rm node_modu ...

  6. laravel 视图流程控制,if switch for loop

    流程控制 除了模板继承和数据显示之外,Blade 还为常用的 PHP 流程控制提供了便利操作,例如条件语句和循环,这些快捷操作提供了一个干净.简单的方式来处理 PHP 的流程控制,同时保持和 PHP ...

  7. Redis Java连接池调研

    Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...

  8. TensorFlow使用记录 (七): BN 层及 Dropout 层的使用

    参考:tensorflow中的batch_norm以及tf.control_dependencies和tf.GraphKeys.UPDATE_OPS的探究 1. Batch Normalization ...

  9. css引入第三方字体

    上面图片时将字体文件放入到fonts文件夹内, 里面有一个fonts.css文件,将字体文件声明好, 然后像下面图片一样,在另外一个css内@import引入,(当然,也可以直接将声明和引用放在一个c ...

  10. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...