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:

  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')

思路分析:

  动态规划的思想,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(编辑距离)的更多相关文章

  1. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶

    sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...

  2. [LeetCode] Edit Distance 编辑距离

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

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

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

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

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

  5. leetcode72. Edit Distance(编辑距离)

    以下为个人翻译方便理解 编辑距离问题是一个经典的动态规划问题.首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离). 状态转换方程有两种情况:边界 ...

  6. 【LeetCode每天一题】Edit Distance(编辑距离)

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

  7. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  8. edit distance(编辑距离,两个字符串之间相似性的问题)

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

  9. 72. Edit Distance(编辑距离 动态规划)

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

随机推荐

  1. 08-求解Ax=b:可解性和解的结构

    一.增广矩阵 假设我们要求解方程$Ax=b$,其中矩阵$A$和$b$如下所示: $A = \left[\begin{array}{llll}{1} & {2} & {2} & ...

  2. AMBS

    AMBS-PMT-TOT-AMT-DUE   -包括往期没还清的 AMBS-PMT-CURR-DUE是单单这一期新欠的 AMBS common used fields: AMBS-BILLING-CY ...

  3. JAVA四种引用方式

    JAVA四种引用方式: java.lang.ref: 强引用(直接变量赋值) 软引用(SoftReference): 只有在要发生OOM错误之前才会回收掉老的软引用对象,应用场景主要防止内存溢出.(缓 ...

  4. jinfo 干涉java runtime的jvm参数

    https://blog.csdn.net/bolg_hero/article/details/78156311 jinfo使用介绍可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时, ...

  5. jquery用法第二波

    过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $(“#id”) $("div[title=test]")选取titl ...

  6. centos 配置vlan

    https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/sec-c ...

  7. cenos中的软件安装

    在linux中安装flash:  http://jingyan.baidu.com/article/fa4125accdeeec28ad709252.html linux java环境的搭建:

  8. PHP截取字符串函数,根据dede修改而来

    dede中,有一个函数function cn_substr_utf8($str, $length, $start=0) 但测试时,并不如我所想的一样,可能是因为个人使用习惯吧.比如,字符串为数字或字母 ...

  9. 用jquery实现图片轮播

    用jquery简单实现图片轮播效果,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 D Merchandise (斜率优化)

    Description: The elderly aunts always like to look for bargains and preferential merchandise. Now th ...