题目:

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. poj 3368 rmq ***

    题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. #include<cstdio> #include<iostream> #incl ...

  2. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp

    Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...

  3. VMware 使用本机代理上网

    灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置

  4. rabbitmq的发布确认和事务 - 2207872494的个人空间

    rabbitmq的发布确认和事务 - 2207872494的个人空间   https://my.oschina.net/lzhaoqiang/blog/670749

  5. WebClient 通过get和post请求api

    //get 请求        string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b ...

  6. a标签连接空标签的方法

    在写页面时,想把a标签设置成空链接,方便后面数据的连接可以有几种方法. 1. <a herf=""></a> 这种方法会默认打开本页面,重新刷新一次页面. ...

  7. HTTP协议GET和POST的区别

    from http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/f ...

  8. [Winform]js与webbrowser交互

    摘要 目前项目中采用的方式是内嵌浏览器的方式,打开本地或者互联网上的h5页面.在开发之前做了一下调研.目前常用的在C#封装的浏览器内核中,Chromium 内核封装有Xilium.Cefglue.Ce ...

  9. 使用Axure RP原型设计实践08,制作圆角文本框

    本篇体验做一个简单圆角文本框,做到3个效果: 1.初始状态,圆角文本框有淡淡的背景色,边框的颜色为浅灰色2.点击圆角文本框,让其获取焦点,边框变成蓝色,背景色变成白色3.圆角文本框失去焦点,边框变成红 ...

  10. Nginx 的动态 upstreams

    原文地址: http://www.oschina.net/translate/nginx-with-dynamic-upstreams 我最近在工作中做一个设置,我有一个面向用户的 Nginx 服务, ...