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 ...
随机推荐
- Linux就该这么学04学习笔记
今天开始学习,开始做笔记,希望自己能坚持下去 参考链接:https://www.linuxprobe.com/chapter-04.html vim编辑器 Linux系统中通用的文本编辑器 vi的升级 ...
- Linux性能优化从入门到实战:12 内存篇:Swap 基础
内存资源紧张时,可能导致的结果 (1)OOM 杀死大内存CPU利用率又低的进程(系统内存耗尽的情况下才生效:OOM 触发的时机是基于虚拟内存,即进程在申请内存时,如果申请的虚拟内存加上服务器实际已用的 ...
- spl_autoload_register() 函数实现的自动加载
和Python用module来区分代码块不同,PHP按照命名空间来区分,开始学PHP的时候一心认定了如果想用 use 关键字来导入(Python的习惯说法)一个类或者函数或者其他对象的话,必须先inc ...
- window 批处理脚本获取上级目录
1 SET CurrDir=%CD% CD.. SET InstPath=%CD% CD %CurrDir% 2 pushd.. set parent=%cd% popd 参考: https://ms ...
- Vue自定义指令实现input限制输入正整数
directive.js import Vue from 'vue' export default () => { Vue.directive('Int', { inserted: functi ...
- Django的下载和基本指令
1.下载Django pip3 install django #不写版本号的话,默认使下载最新版的django pip3 install django == 2.1.2 #指定 ...
- Java泛型与集合笔记
第一章 Java的泛型为了兼容性和防止代码爆炸,在编译成字节碼时会进行类型擦除,编译器自动添加代码做类型转换(用到List<Integer>的地方用Integer来做转换),自动做装箱拆箱 ...
- ECS主动运维事件--让你HOLD住全场 (二)
背景 数月前,我们推出了新的功能:ECS主动运维事件--让你HOLD住全场 https://yq.aliyun.com/articles/573782?spm=a2c4e.11155435.0.0.7 ...
- 记录一下LEETheme库主题切换的使用
LEETheme库的位置:https://github.com/lixiang1994/LEETheme 从https://github.com/gsdios的SDAutolayoutDemo 中的白 ...
- 16 :IDEA快速键
ctrol+z ctrol+shift+z 重做 复制,粘贴,删除,(行操作,光标放在那里就可以操作,不要全选择) 注:特别:查询出来,文件是可以直接编辑的 crtol+F double +shif ...