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. C++入门经典-例3.6-判断某一年是否是闰年之复合表达式法

    1:代码如下: // 3.6.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  2. sudo密码一直出错

    Linux默认是没有将用户添加到sudoers列表中的,需要root手动将账户添加到sudoers列表中,才能让普通账户执行sudo命令. 所以要将用户添加到sudoers组中,才能执行sudo命令, ...

  3. watir安装——windows环境

    默认情况下,gem sources 都是https://rubygems.org/

  4. PDS常用快捷键

    绿色在Layout和Router中共用 1.PDS常用快捷键:2019-07-28 17:06:07 快捷键 说明 备注 shiftt + 左键双击 布线状态下,进行过孔放置   ctrl + 左键双 ...

  5. 浏览器端-W3School-JavaScript:History 对象

    ylbtech-浏览器端-W3School-JavaScript:History 对象 1.返回顶部 1. History 对象 History 对象 History 对象包含用户(在浏览器窗口中)访 ...

  6. 深入解析CNN pooling 池化层原理及其作用

    原文地址:https://blog.csdn.net/CVSvsvsvsvs/article/details/90477062 池化层作用机理我们以最简单的最常用的max pooling最大池化层为例 ...

  7. inner join, left join, right join, full outer join的区别

    总的来说,四种join的区别可以描述为: left join 会从左表(shop)那里返回所有的记录,即使在右表(sale_detail)中没有匹配的行. right outer join 右连接,返 ...

  8. 第九章 SpringCloud之Zuul路由

    ############Zuul简单使用################ 1.pom.xml <?xml version="1.0" encoding="UTF-8 ...

  9. 报错:Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET

    问题:Appium的android真机启动手机时,会遇到以下问题: An unknown server-side error occurred while processing the command ...

  10. linu基础命令1

    /根目录,第一级目录 1.ls列出当前目录下的文件和目录-a: 列出所有的文件,包括所有以.开头的隐藏文件-d: 列出目录本身,并不包含目录中的文件(-ld)-h: 和-l一起使用,文件大小人类易读 ...