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:

  1. Insert a character
  2. Delete a character
  3. 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')
 

使用递归会造成Time limit exceeded

class Solution {
public int minDistance(String word1, String word2) {
StringBuffer strBuf1 = new StringBuffer(word1);
StringBuffer strBuf2 = new StringBuffer(word2); return dfs(strBuf1,strBuf2,0,0,0);
} public int insert(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
strBuf1.insert(i1, strBuf2.charAt(i2));
int ret = dfs(strBuf1,strBuf2,i1+1, i2+1,depth+1);
strBuf1.deleteCharAt(i1); //recover
return ret;
} public int delete(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
Character ch = strBuf1.charAt(i1);
strBuf1.deleteCharAt(i1);
int ret = dfs(strBuf1,strBuf2,i1, i2,depth+1);
strBuf1.insert(i1,ch); //recover;
return ret;
} public int replace(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
Character ch = strBuf1.charAt(i1);
strBuf1.setCharAt(i1, strBuf2.charAt(i2));
int ret = dfs(strBuf1,strBuf2,i1+1, i2+1,depth+1);
strBuf1.setCharAt(i1, ch);
return ret;
} private int dfs(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
while(i1 < strBuf1.length() && i2 < strBuf2.length() && strBuf1.charAt(i1) == strBuf2.charAt(i2)){
i1++;
i2++;
} if(i1 == strBuf1.length() && i2 == strBuf2.length()) return depth;
if(i1 == strBuf1.length()) return depth+strBuf2.length()-i2;
if(i2 == strBuf2.length()) return depth+strBuf1.length()-i1; int ret = insert(strBuf1,strBuf2,i1,i2,depth);
ret = Math.min(ret,delete(strBuf1,strBuf2,i1,i2,depth));
ret = Math.min(ret,replace(strBuf1,strBuf2,i1,i2,depth)); return ret; } }

使用动态规划dp[i][j]表示从word1[i+1]位置到word2[j+1]位置 需要改变次数。

class Solution {
public int minDistance(String word1, String word2) {
int[][] dp = new int[word1.length()+1][word2.length()+1];
for(int i = 0; i <= word1.length(); i++){
dp[i][0] = i;
}
for(int j = 0; j <= word2.length(); j++){
dp[0][j] = j;
}
for(int i = 1; i <= word1.length(); i++){
for(int j = 1; j <= word2.length(); j++){
if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}
else{
dp[i][j] = 1+Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1])); //insert & replace: dp[i-1][j-1] +1; delete: dp[i-1][j],dp[i][j-1]
}
}
} return dp[word1.length()][word2.length()];
} }

72. Edit Distance (JAVA)的更多相关文章

  1. 【Leetcode】72 Edit Distance

    72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...

  2. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  3. 72. Edit Distance

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

  4. [LeetCode] 72. Edit Distance 编辑距离

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

  5. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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

  6. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  7. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  8. 72. Edit Distance *HARD*

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

  9. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

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

随机推荐

  1. 【学习】SpringBoot之简介、特点、缺点、应用场景

    Spring Boot 的介绍 SpringBoot的目的在于创建和启动新的基于Spring框架的项目.Spring Boot 会选择最合适的Spring子项目和第三方开源库进行整合.大部分Sprin ...

  2. 过滤器修改response

    过滤器通过doFilter方法的第二个参数ServletResponse将输出发送给客户,但servletResponse参数没有为过滤器提供servlet或jsp页面的访问:执行doFilter方法 ...

  3. maven仓库,snapshot快照仓库和release发布仓库的区别

    首先看下snapshot类型 建立一个maven-privider项目,版本定义为1.1-SNAPSHOT 创建一个privider类,写一个hello()方法,并推送maven私有仓库 然后再建立一 ...

  4. Django中csrf token验证原理

    我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...

  5. BOSCH汽车工程手册————驾驶员辅助系统

    根据交通事故统计得出平均每分钟有一人死于交通事故 而辅助驾驶系统能够为驾驶员洞察了解汽车周围情况,识别危险的行驶状况. 提早为驾驶员告诉危险信息,可减少60%汽车驶上主路事故和1/3汽车前碰事故. 有 ...

  6. Anaconda 32在windows下安装gensim

    安装Anaconda 2.4以后运行corpora.MmCorpus.serialize的时候出错 换了Anaconda 2.1以后没问题了 原因:Anaconda 2.4的numpy是1.10.1版 ...

  7. Typography 字体

    Typography 字体 我们对字体进行统一规范,力求在各个操作系统下都有最佳展示效果. ¶中文字体 和畅惠风 PingFang SC 和畅惠风 Hiragino Sans GB 和畅惠风 Micr ...

  8. 使用jedis执行lua脚本

    转: redis学习(十五) 使用jedis执行lua脚本(实现一个对IP的限流) 2018年09月15日 20:07:26 码农-文若书生 阅读数:1609   使用jedis执行lua脚本(实现一 ...

  9. web开发(四) 一次性验证码的代码实现

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6426072.html>,在此仅供学习参考之用. 其实实现 ...

  10. 阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合

    创建新项目.不选择骨架 打包方式选择是jar 增加mysql的包依赖 创建demo类来讲解程序的耦合 原来里面提供了sql语句.拿到mysql没执行