71.Edit Distance(编辑距离)
Level:
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')
思路分析:
动态规划的思想,dp[i] [j]代表将word1前i个字符转换成word2前j个字符所需要的操作次数。
如果word1[i]==word2[j],那么dp[i] [j]=dp[i-1] [j-1]。
如果word1[i]不等于word2[j],需要找出插入元素,删除元素,替换元素中最小的操作,然后加一。
dp[i] [j-1]表示word1前i个可以表示word2前j-1个,那么要表示前j个,只能执行插入操作。
dp[i-1] [j]表示word1前i-1个可以表示word2前j个,那么要前i个表示前j个,只能执行删除操作。
dp[i-1] [j-1]表示word1前i-1个可以表示word2前j-1个,那么要前i个表示前j个,只能执行替换操作。
则dp[i] [j]=min(dp[i-1] [j],dp[i] [j],dp[i-1] [j-1])+1;
代码:
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=0;i<=m;i++){
dp[i][0]=i; //单纯的删除操作
}
for(int i=0;i<=n;i++){
dp[0][i]=i; //单纯的插入操作
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(word1.charAt(i)==word2.charAt(j)){
dp[i+1][j+1]=dp[i][j];
}else{
dp[i+1][j+1]=Math.min(dp[i+1][j],Math.min(dp[i][j+1],dp[i][j]))+1;
}
}
}
return dp[m][n];
}
}
71.Edit Distance(编辑距离)的更多相关文章
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- leetcode72. Edit Distance(编辑距离)
以下为个人翻译方便理解 编辑距离问题是一个经典的动态规划问题.首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离). 状态转换方程有两种情况:边界 ...
- 【LeetCode每天一题】Edit Distance(编辑距离)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- edit distance(编辑距离,两个字符串之间相似性的问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 72. Edit Distance(编辑距离 动态规划)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
随机推荐
- 创建entityManager
1 需要persistence.xml 完全通过属性配置没成功 <persistence xmlns="http://java.sun.com/xml/ns/persistence&q ...
- highlight语法高亮推荐样式
最近在弄一个类似博客的东西,需要高亮显示代码,所以用了highlight.js来完成 highlight提供了不同的风格我们可以通过更改css样式表来找到适合我们的. 我罗列一部分看看有哪些风格 以下 ...
- java selenium常用API汇总
(WebElement.iFrame.select.alert.浏览器窗口.事件.js) 一 WebElement相关方法 1.点击操作 WebElement button = driver. ...
- 线程中sleep和wait方法的区别
sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...
- Python PEP8代码书写规范
摘自: 规范 https://blog.csdn.net/ratsniper/article/details/78954852
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...
- [CF1081H]Palindromic Magic
题意:给两个字符串\(a,b\),求出有多少种不同的字符串能通过从第一个串中取出一个回文串,从第二个串中取出一个回文串,按顺序拼接得到. 题解:证明?看官方题解吧 一些定义: 回文串拆分:\(s=ab ...
- C#中给RICHTEXTBOX加上背景图片
在系统自带的RichTextBox中是无法给它设置背景图片,但是我们在某些场合可能需要给RichTextBox设置背景图片.那么怎么实现这一想法呢?经过研究发现通过其它巧妙的途径可以给RichText ...
- php面试专题---MySQL分表
php面试专题---MySQL分表 一.总结 一句话总结: 分库分表要数据达到一定的量级才用,这样才有效率,不然利不一定大于弊,可能会增加一次I/O消耗 1.分库分表的使用量级是多少? 单表行数超过 ...
- C# 高性能 TCP 服务的多种实现方式Cowboy.Sockets
本篇文章的主旨是使用 .NET/C# 实现 TCP 高性能服务的不同方式,包括但不限于如下内容: APM 方式,即 Asynchronous Programming Model TAP 方式,即 ...