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. HDU 6045 - Is Derek lying | 2017 Multi-University Training Contest 2

    /* HDU 6045 - Is Derek lying [ 分析 ] 题意: 有N个问题, 每个问题有A,B,C三种答案,答对加一分,答错不加分 给出甲乙两人的答案,给出两人的分数先x, y,问分数 ...

  2. windows环境下,mysql的root密码丢失后重置方法

    运行窗口输入 services.msc,检查mysql服务是否启动,如果启动手动停止或输入 net stop mysql 停止msyql服务.   打开cmd命令行,使用cd命令进入mysql 的bi ...

  3. 使用matplotlib绘制常用图表(3)-其他图表绘制

    一.绘制三点图 """ 三月份最高气温 a = [12,15,18,6,7,5,6,8,9,10,15,10,4,5,11,10,5,6,12,15,10,5,14,10 ...

  4. 学习andriod开发之 异步加载图片(二)--- 使用其他进度条

    大家好 我是akira上一节 我们讲到使用AsyncTask 这个类进行异步的下载 主要是涉及到一些图片的更新 这次我们继续上一个demo的改进 . 不知道你是否发现一个问题 上一节我们遗留了两个bu ...

  5. P1427 小鱼的数字游戏

    输入格式: 一行内输入一串整数,以0结束,以空格间隔. 输出格式: 一行内倒着输出这一串整数,以空格间隔. 直接上代码: #include<iostream> using namespac ...

  6. MySQL_8.0与5.7区别之账户与安全

    一.创建用户和用户授权 MySQL5.7创建用户和用户授权命令可以同时执行 grant all privileges on *.* to 'Gary'@'%' identified by 'Gary@ ...

  7. CodeForces 724C Ray Tracing(碰撞类,扩展gcd)

    又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...

  8. Xshell远程连接的具体操作和Xshell多会话设置小技巧

    前几天给大家分享了Xshell的安装教程,今天给大家分享如何在Xshell中进行远程连接,并且分享一下如何设置一条命令可以发送多个终端,这里以Xshell6为例进行说明,具体的教程如下. 1.依次点击 ...

  9. python-pyhs2

    #!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive server2 """ @auth ...

  10. [CSP-S模拟测试]:那一天她里我而去(堆优化Dijkstra)

    题目传送门(内部题3) 输入格式 每个测试点有多组测试数据.第一行有一个正整数T表示数据组数.接下来对于每组数据,第一行有两个正整数n,m分别代表图的点数和边数.接下来有m行,每行三个整数u,v,d表 ...