题目:

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character

c) Replace a character

题解

处理这道题也是用动态规划。

动态数组dp[word1.length+1][word2.length+1]

dp[i][j]表示从word1前i个字符转换到word2前j个字符最少的步骤数。

假设word1现在遍历到字符x,word2遍历到字符y(word1当前遍历到的长度为i,word2为j)。

以下两种可能性:

1. x==y,那么不用做任何编辑操作,所以dp[i][j] = dp[i-1][j-1]

2. x != y

(1) 在word1插入y, 那么dp[i][j] = dp[i][j-1] + 1

(2) 在word1删除x, 那么dp[i][j] = dp[i-1][j] + 1

(3) 把word1中的x用y来替换,那么dp[i][j] = dp[i-1][j-1] + 1

最少的步骤就是取这三个中的最小值。

最后返回 dp[word1.length+1][word2.length+1] 即可。

代码如下:

 1 public static int minDistance(String word1, String word2) {
 2     int len1 = word1.length();
 3     int len2 = word2.length();
 4  
 5     // len1+1, len2+1, because finally return dp[len1][len2]
 6     int[][] dp = new int[len1 + 1][len2 + 1];
 7  
 8     for (int i = 0; i <= len1; i++) 
 9         dp[i][0] = i;
     
     for (int j = 0; j <= len2; j++) 
         dp[0][j] = j;
     
  
     //iterate though, and check last char
     for (int i = 1; i <= len1; i++) {
         char c1 = word1.charAt(i-1);
         for (int j = 1; j <= len2; j++) {
             char c2 = word2.charAt(j-1);
  
             //if last two chars equal
             if (c1 == c2) {
                 //update dp value for +1 length
                 dp[i][j] = dp[i-1][j-1];
             } else {
                 int replace = dp[i-1][j-1] + 1;
                 int insert = dp[i-1][j] + 1;
                 int delete = dp[i][j-1] + 1;
  
                 int min = Math.min(replace, insert);
                 min = Math.min(min,delete);
                 dp[i][j] = min;
             }
         }
     }
  
     return dp[len1][len2];
 }

Reference:

http://www.programcreek.com/2013/12/edit-distance-in-java/

http://blog.csdn.net/linhuanmars/article/details/24213795

Edit Distance leetcode java的更多相关文章

  1. edit distance leetcode

    这样的字符转换的dp挺经典的, 若word1[i+1]==word2[j+1] dp[i+1][j+1] = dp[i][j]:否则,dp[i+1][j+1] = dp[i][j] + 1.(替换原则 ...

  2. Java for LeetCode 072 Edit Distance【HARD】

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

  3. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  4. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  5. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  6. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  7. [LeetCode] Edit Distance 编辑距离

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

  8. [leetcode]161. One Edit Distance编辑步数为一

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

随机推荐

  1. Lambda的分类(语句Lambda和表达式Lambda)

    学习自 <C#本质论> Overview 在上一文中,我们简而又简的了解了一下,匿名方法和Lambda表达式,关于匿名方法这里暂且不表,本文我们来更加详细的了解一下Lambda表达式. 本 ...

  2. Java 操纵XML之读取XML文件

    Java 操纵XML之读取XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  3. BZOJ.1901.Dynamic Rankings(整体二分)

    题目链接 BZOJ 洛谷 (以下是口胡) 对于多组的询问.修改,我们可以发现: 假设有对p1,p2,p3...的询问,在这之前有对p0的修改(比如+1),且p0<=p1,p2,p3...,那么我 ...

  4. Kruskal 模板

    最小生成树指的是在图上面找到权值最小的一棵树,并且保证图上所有的点都在这棵树上. 解决办法:Kruskal 算法(贪心思想) 将边按权值从小到大排序,然后按这个顺序不断连边,直到所有点联通. /** ...

  5. 硬盘img镜像备份工具

    linux下使用dd,Windows下使用WinImage,不过dd使用的比较多.

  6. 使用GIT进行源码管理——GIT托管服务2018

    我曾经介绍过几个在线的GIT托管服务,然而时过境迁,发生了不少变化,便写了此文章,在新的一年重新更新一下:   国外托管网站: 国外托管网站比起国内的来相对靠谱点,但一个主要缺点是网速较慢,并且可能在 ...

  7. SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic

    SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic The SW-DP protocol is described ...

  8. SourceTree 的初次使用的两个小问题

    菜鸟才开始使用SourceTree,出现了两个小问题,特此整理一下,希望对各位新手有帮助.刚开始以为装了SourceTree就不用装git了,其实不然,不装git就会出现下面第一个问题: 1.新手使用 ...

  9. [Go] panic 和 recover

    通常情况下,函数向其调用方报告错误的方式都是返回一个 error 类型的值.但是,当遇到致命错误的时候,很可能会使程序无法继续运行.这时,上述错误处理方式就太不适合了,Go 推荐通过调用 panic ...

  10. C#访问远程主机资源的方法,多种方式

    最近要实现访问远程主机的共享目录中的一个文件.遇到了权限问题.google了一下,找到了几种解决方法,记录如下: 一.调用Net use命令 // 使用方法:        //if (Connect ...