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. opencv_将图像上的4个点按逆时针排序

    1:代码如下: #include "stdafx.h" #include "cxcore.h" #include "cvcam.h" #in ...

  2. java 多线程相关概念总结

    前言 本篇文章介绍一些多线程的相关的深入概念.理解后对于线程的安全性会有更深的理解. 先说一个格言,摘自Java核心技术:如果向一个变量写入值,而这个变量接下来可能会被另一个线程读取:或者一个变量读值 ...

  3. extentsreport testng美化报告生成

    一:主要内容 优化testng测试报告,使用extentsreport 解决extentsreport打开后加载不出来样式的问题 二:报告效果 先上图,看下testng extentsreport报告 ...

  4. android studio中方法和类被调用多次,但是AS显示灰色,解决办法

    Android Studio里面的一些类及方法,明明有被其他的类或者方法调用,但是去看的时候显示灰色,鼠标放上面的时候显示:Class ‘XXX’ is never used或者Method ‘XXX ...

  5. React之defaultProps、propTypes

    1.新增知识点 /** React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. 父子组件:组件的相互调用中,我们把调 ...

  6. 【Java学习笔记】LinkedList JDK1.6

    如下一段代码,在JDK1.6的LinkedList中,是下图这样存储的.有一个节点值为null的节点,叫做header,header的next是0,3的next是header,这是一个循环链表 Lin ...

  7. Cocos2d-X多线程(2) 线程的互斥量std::mutex和线程锁

    多个线程同时访问共享资源时,经常会出现冲突等.为了避免这种情况的发生,可以使用互斥量,当一个线程锁住了互斥量后,其他线程必须等待这个互斥量解锁后才能访问它. thread提供了四种不同的互斥量: 1. ...

  8. Cocos2d-X多线程(1) 在cocos2d-x中使用多线程

    教科书上说:进程是资源分配的最小单位,线程是CPU调度的最小单位. 进程是程序在计算机上的一次执行活动.直观的讲就是会产生一个pid. int main() {     //业务逻辑代码     re ...

  9. Cocos2d-X网络编程(5) 使用Rapidjson解析数据

    Json基础及28种c++解析库性能对比 JSON 概念和特点:     JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)     JSON ...

  10. mongodb 数据库操作 -- 》常用命令

    首先需要下载数据库,安装后,找到bin目录,点开bin目录,复制当前路径配置到环境变量中 和bin的同级下,需要建立一个data/db文件夹,该文件夹并不会自动生成,必须手动设置   启动数据库  看 ...