LeetCode之“动态规划”:Edit Distance

  题目链接

  题目要求:

  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

  该题的解析宅自一博文

  我们维护的变量res[i][j]表示的是word1的前i个字符和word2的前j个字符编辑的最少操作数是多少。假设我们拥有res[i][j]前的所有历史信息,看看如何在常量时间内得到当前的res[i][j],我们讨论两种情况:
  1)如果word1[i-1]=word2[j-1],也就是当前两个字符相同,也就是不需要编辑,那么很容易得到res[i][j]=res[i-1][j-1],因为新加入的字符不用编辑;
  2)如果word1[i-1]!=word2[j-1],那么我们就考虑三种操作,如果是插入word1,那么res[i][j]=res[i-1][j]+1,也就是取word1前i-1个字符和word2前j个字符的最好结果,然后添加一个插入操作;如果是插入word2,那么res[i][j]=res[i][j-1]+1,道理同上面一种操作;如果是替换操作,那么类似于上面第一种情况,但是要加一个替换操作(因为word1[i-1]和word2[j-1]不相等),所以递推式是res[i][j]=res[i-1][j-1]+1。上面列举的情况包含了所有可能性,有朋友可能会说为什么没有删除操作,其实这里添加一个插入操作永远能得到与一个删除操作相同的效果,所以删除不会使最少操作数变得更好,因此如果我们是正向考虑,则不需要删除操作。取上面几种情况最小的操作数,即为第二种情况的结果,即res[i][j] = min(res[i-1][j], res[i][j-1], res[i-1][j-1])+1。

  程序大概流程图如下:

  

  程序如下:

 1 class Solution {
 2 public:
 3     int min(int a, int b, int c)
 4     {
 5         int tmp = (a < b) ? a : b;
 6         return (tmp < c) ? tmp : c;
 7     }
 8
 9     int minDistance(string word1, string word2) {
10         int szWord1 = word1.size();
11         int szWord2 = word2.size();
12         if(szWord1 == 0)
13             return szWord2;
14         if(szWord2 == 0)
15             return szWord1;
16
17         vector<vector<int> > dp(szWord1 + 1, vector<int>(szWord2 + 1, 0));
18         for(int i = 0; i < szWord1 + 1; i++)
19             dp[i][0] = i;
20         for(int j = 0; j < szWord2 + 1; j++)
21             dp[0][j] = j;
22
23         for(int i = 1; i < szWord1 + 1; i++)
24             for(int j = 1; j < szWord2 + 1; j++)
25             {
26                 if(word1[i-1] == word2[j-1])
27                     dp[i][j] = dp[i-1][j-1];
28                 else
29                     dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1;
30             }
31
32         return dp[szWord1][szWord2];
33     }
34 };
时间: 2024-10-02 01:29:09

LeetCode之“动态规划”:Edit Distance的相关文章

Baozi Leetcode solution 72. Edit Distance

Problem Statement 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: Insert a character Delete a character Replace a character Example 1:

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

Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到的. 在这里需要注意几点: 1.不等于1的变换都要返回false(包括变换次数等于0). 2.还有很多细节需要注意. 方法如下: 1.直接判断:1)如果差值大于1,直接返回false.  2)如果长度相同,那么依次判断,是否只有一个字母不一样.  3)如果不一样,那么看是否是只是多出了一个字母. p

(LeetCode 72)Edit Distance

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 characterb) Delete a characterc) Replace

【一天一道LeetCode】#72. Edit Distance

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 f

【LeetCode 72】Edit Distance

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 characterb) Delete a characterc) Replace

[Leetcode 72]编辑距离 Edit Distance

[题目] 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: Insert a character Delete a character Replace a character Example 1: Input: word1

leetcode[161] One Edit Distance

判断两个字符串的编辑距离是不是1. 两个字符串距离是1的可能性: 1)两个字符串长度相等:s="abc",t="aec"; 2)两个字符串长度相差1(两种情况):s="abc",t="abce";或s="abc",t="aebc"; bool isOneEditDistance(string s, string t) { if(s.length()>t.length())swap(

LeetCode – Refresh – One Edit Distance

Scanning from start to end. If find a mismatch and one is larger size, keep search from the previous char of shorter one. Finally check whether found a mismatch OR still have a larger size string. 1 class Solution { 2 public: 3 bool isOneEditDistance

leetcode || 72、Edit Distance

problem: 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

动态规划——Edit Distance

大意:给定两个字符串word1和word2,为了使word1变为word2,可以进行增加.删除.替换字符三种操作,请输出操作的最少次数 Example 1: Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove '