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 ...
随机推荐
- 随笔2 AbstractMap<K,V>
上一篇写了Map接口的源码分析,这一篇写一下Map接口的一个实现类AbstractMap,从名字就可以看出这是一个抽象类,提供了Map接口的骨架实现,为我们实现Map接口的时候提供了很大的便利.在这里 ...
- [BZOJ] 地精部落
问题描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H 可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中 Hi ...
- SpringBoot整合MyBatis-Plus实现快速业务功能开发
概览:使用MybatisPlus和它的代码生成整合SpringBoot可以实现快速的业务功能开发,具体步骤如下 一.添加依赖 <dependency> <groupId>org ...
- ht-4 hashmap特性
一.hashmap底层原理: hashmap调用默认构造方法会产生一个默认底层是长度为16的Entry数组,首先调用key的hasCode()方法来得到一个整数, int hash = hash(ke ...
- discuz x3.2设置注册邮件激活_企业邮箱发送邮件失败
在discuz x2.5邮箱设置里面已经说了很多关于邮件设置和常见问题的处理办法了,今天这里主要是说明下Discuz! 邮件发送失败排查思路,适用于任何板块的Discuz程序. Discuz! 邮件发 ...
- 【Java】Java URLDecoder异常Illegal hex characters in escape (%)
如果收到的HTTP请求参数(URL中的GET请求)中有一个字符串,是中文,比如“10%是黄段子”,服务器段使用URLDecoder.decode就会出现此异常.URL只能使用英文字母.阿拉伯数字和某些 ...
- vue中操作Dom节点的方法
1.vue中ref操作dom节点 <template> <div id="app"> <div </div> <button @cl ...
- Uva 12563 Jin Ge Jin Qu hao(01背包)
题意: 假定你在唱KTV,还剩下t秒时间.你决定接下来唱你最喜爱的n首歌(不包含劲歌金曲)中的一些歌曲.在时间结束之前再唱一个劲歌金曲.使得唱的歌的总曲目尽量多以及时间总长度. 输入保证所有n+1曲子 ...
- Jupyter Notebook 快捷键(基本)
Jupyter Notebook 快捷键 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文本:这时的单元框线是绿色的.命令模式,键盘输入运行程序命令:这时的单 ...
- Windows 下开启FTP服务并创建FTP用户
Windows 下开启FTP服务,并创建用户 此教程教你怎么开启 Windows 的 FTP 服务,并创建用于登入 FTP 的用户.教程用到的操作系统是 Windows 7. 一.创建用于登入 FTP ...