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

题意:

给定两个串串: word1, word2;  找出word1变身成word2的最小步数的操作(仅限插入、删除和替换这三种操作)

思路:

动态规划的高频题,需要熟练掌握。

字符串生成子序列或者字符串的匹配问题,要巴普诺夫条件反射想到dp。

开一个2D array, dp[word1.length() + 1 ][ word2.length() + 1]

      word2 = 0  r o s

         0   0   

word1 =  h

         o

         r

         s

         e

用dp[i][j]来记录当前word1变身成word2的最小步数

1.    若word1.charAt(i-1) == word2.charAt(j-1) 【留心字符串的index和2D array的坐标有差,这里很容易误写成word1.charAt(i) == word2.charAt(j) 】

  dp[i][j] = dp[i-1][j-1]

  即若当前word1串串和word2串串的当前字符相同,则不需要做任何convert操作,直接将之前dp[i-1][j-1] 结果拿过来

2.    若word1.charAt(i-1) == word2.charAt(j-1)

  dp[i][j] = min( dp[i-1][j-1],  dp[i-1][j],  dp[i][j-1] )  + 1

即若当前word1串串和word2串串的当前字符不同, 则

要么word1插入 word2当前的字符, dp[i][j] = dp[i][j-1] + 1

  要么word1删除 word1当前的字符, dp[i][j] = dp[i-1][j] + 1

要么word1替换 word2当前的字符, dp[i][j] = dp[i-1][j-1] + 1

取以上三个操作中最小的步数

代码:

 class Solution {
public int minDistance(String s1, String s2) {
int[][]dp = new int[s2.length() + 1][s1.length() + 1];
dp[0][0] = 0;
for(int i = 1; i<=s2.length(); i++){
dp[i][0] = i;
}
for(int j = 1; j<=s1.length(); j++){
dp[0][j] = j;
}
for(int i = 1; i<=s2.length(); i++){
for(int j = 1; j<=s1.length(); j++){
if( s1.charAt(j-1) == s2.charAt(i-1) ){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i][j-1] , dp[i-1][j] )) + 1 ;
}
}
}
return dp[s2.length()][s1.length()];
}
}

[leetcode]72. Edit Distance 最少编辑步数的更多相关文章

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

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

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

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

  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 (hard)

    原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...

  5. 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

    Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...

  6. LeetCode - 72. Edit Distance

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

  7. 【Leetcode】72 Edit Distance

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

  8. 刷题72. Edit Distance

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

  9. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

随机推荐

  1. PythonStudy——格式化输入小练习

    # 练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式# ------------ info of Egon -----------# Name : Egon# Age : 22# Sex : ...

  2. GoStudy——解决vscode中golang插件依赖安装失败问题

    vscode中安装ms-vscode.go插件后可以开启对go语言的支持,ms-vscode.go插件需要依赖一些工具,安装完成后提示 Installing github.com/nsf/gocode ...

  3. Session、Cookie、Cache、Token分别是什么及区别

    一.Session 1 )Session 解释 Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cooki ...

  4. Linux之目录结构配置

    因为 Linux 的开发者实在太多了,如果每个人都发展出属于自己的目录配置方法, 那么将可能会造成很多管理上的困扰.所以,就有一个叫做Filesystem Hierarchy Standard (FH ...

  5. Git-撤销(回退)已经add,commit或push的提交

    本文只阐述如何解决问题,不会对git的各种概念多做介绍,如果有兴趣可以点击下面的链接,进行详细的学习:Pro Git本文适用的环境 现在先假设几个环境,本文将会给出相应的解决方法:1. 本地代码(或文 ...

  6. Bootstrap 前端UI框架

    Bootstrap 有哪些优越性? 1.简单灵活的用于搭建WEB页面的HTML,CSS, JavaScript的工具集 2.基于html5, css3, 具有良好特性,友好的学习曲线,卓越的兼容性,1 ...

  7. Node安装及自定义config

    下载Node.js , Windows下安装一键到底, 没有什么说的. 主要是默认无法流畅使用, 包括流畅下载, 全局缓存之类. 如下是进一步设置, 包括: - 设置淘宝镜像. - 增加Yarn(np ...

  8. java的String的乱码浅析

    Java又乱码了,怎么办:乱码了说明编码与解码不一致导致.所以使用统一的编码方式即可. 本文并不是一定能解决乱码,本文主要用来了解jvm默认编码,以及string编码与解码一致性问题. jvm的默认编 ...

  9. NIOS II With uCOSII

    1.如果使用uCOS,那么Qsys中Nios II核就不能使用外部中断控制器(EIC). 2.遇到很迷惑的问题,运行uCOSII的实例代码,总是在第二个OSTimeDlyHMSM(0, 0, 3, 0 ...

  10. svn Mac

    将已有项目放到svn服务端 svn import 已有项目地址 服务端地址 -m '注释必须填写' 例子 svn import /Applications/Emma/workspace/tansun/ ...