leetcode Some questions of Edit Distance

leetcode72 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

分析

这是一道典型的计算编辑距离的题目,何为编辑距离呢?
编辑距离是将一个序列通过插入、删除、替换变为另一个序列所需要的执行插入、删除、替换的次数。
思考通过动态规划来解决,设需要计算编辑距离的两个字符串分别是x[1…m]和y[1…n],用 E[i][j] 表示 x[1…i] 和 y[1…j] 的编辑距离。可以得到状态转移放入如下:

if (i == 0) E(i, j) = j;
if (j == 0) E(i, j) = i;
if (i > 0 && j > 0) E(i, j) = min{1 + E(i - 1, j), 1 + E(i, j - 1), diff(i, j) + E(i - 1, j - 1)}   //if x[i] == y[j], diff(i, j) = 0, else diff(i, j) = 1
  • 当 i == 0和j == 0,状态转移方程很好理解,当一个字符串是空串,将另一个字符串转化为空串只能将其所有字符删除,这样编辑距离就是另一个字符串的长度。
  • 当i != 0且j != 0
    • 如果通过删除、插入、替换之后,可以得到E(i - 1, j),即使x(1…i - 1) == y(1…j)需要进行的操作,此时对于E(i, j),因为E(i - 1, j)已经得到,可以知道只需将x[i]删除即可
    • 如果通过删除、插入、替换之后,可以得到E(i, j - 1),即使x(1…i) == y(1…j - 1)需要的操作次数,此时对于E(i, j),可以知道只需要在x(1…i)后插入y[j]即可
    • 如果通过删除、插入、替换之后,可以得到E(i - 1, j - 1),如果x[i] == y[j],此时,不需要进行任何操作,此时E(i, j) == E(i - 1, j - 1),若x[i] != y[j],此时只需要将x[i]替换为y[j]

代码

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.size(), n = word2.size();
        if (m == 0) return n;
        if (n == 0) return m;
        int result[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
          for (int j = 0; j <= n; j++) {
            if (i == 0) {
              result[i][j] = j;
            } else if (j == 0) {
              result[i][j] = i;
            } else {
              int temp = min(1 + result[i - 1][j], 1 + result[i][j - 1]);
                cout << temp << endl;
              if (word1[i - 1] == word2[j - 1]) {
                result[i][j] = min(temp, result[i-1][j - 1]);
              } else {
                result[i][j] = min(temp, 1 + result[i - 1][j - 1]);
              }
            }
          }
        }
        return result[m][n];
    }
};

运行结果

这里写图片描述

leetcode583 Delete Operation for Two Strings

题目

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

Note
1. The length of given words won’t exceed 500.
2. Characters in given words can only be lower-case letters.

分析

可以看到,这道题目相对与上面一道题,略有改变,首先是只允许通过删除来使两个字符串相同,其次是删除操作可以在两个字符串中执行。
虽然题目不太相同,但是,思路还是相同的。
设需要计算编辑距离的两个字符串分别是x[1…m]和y[1…n],用 E[i][j] 表示 x[1…i] 和 y[1…j] 的编辑距离。状态转移方程如下:

if (i == 0) {
  result[i][j] = j;
} else if (j == 0) {
  result[i][j] = i;
} else {
  int temp = min(1 + result[i - 1][j], 1 + result[i][j - 1]);
  if (word1[i - 1] == word2[j - 1]) {
    result[i][j] = min(temp, result[i - 1][j - 1]);
  } else {
    result[i][j] = min(temp, 2 + result[i - 1][j - 1]);
  }
}

可以看到,唯一的不同是第三种情况:
该种情况在该题中的描述改为,如果通过删除之后,可以得到E(i - 1, j - 1),如果x[i] == y[j],此时,不需要进行任何操作,此时E(i, j) == E(i - 1, j - 1),若x[i] != y[j],此时需要在x中删除x[i],在y中删除y[j],即需要两次操作。

代码

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.size(), n = word2.size();
        if (m == 0) return n;
        if (n == 0) return m;
        int result[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
          for (int j = 0; j <= n; j++) {
            if (i == 0) {
              result[i][j] = j;
            } else if (j == 0) {
              result[i][j] = i;
            } else {
              int temp = min(1 + result[i - 1][j], 1 + result[i][j - 1]);
              if (word1[i - 1] == word2[j - 1]) {
                result[i][j] = min(temp, result[i - 1][j - 1]);
              } else {
                result[i][j] = min(temp, 2 + result[i - 1][j - 1]);
              }
            }
          }
        }
        return result[m][n];
    }
};

运行结果

这里写图片描述

leetcode712 Minimum ASCII Delete Sum for Two Strings

题目

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.

Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum.  Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

Note:
* 0 < s1.length, s2.length <= 1000.
* All elements of each string will have an ASCII value in [97, 122].

分析

题目与上一题非常相似,只是将编辑距离换成了ASCII码,其实,也只是需要在计算编辑距离的时候,计算的是ASCII码而已。

代码

class Solution {
public:
    int minimumDeleteSum(string s1, string s2) {
        int m = s1.size(), n = s2.size();
        int result[m + 1][n + 1];
        for (int i = 0; i < m + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                if (i == 0) {
                    result[i][j] = 0;
                    for (int k = 0; k < j; k++) {
                        result[i][j] += s2[k];
                    }
                } else if (j == 0) {
                    result[i][j] = 0;
                    for (int k = 0; k < i; k++) {
                        result[i][j] += s1[k];
                    }
                } else {
                    int Min = min(s1[i - 1] + result[i - 1][j], s2[j - 1] + result[i][j - 1]);
                    if (s1[i - 1] == s2[j - 1]) {
                        result[i][j] = min(Min, result[i - 1][j - 1]);
                    } else {
                        result[i][j] = min(Min, result[i - 1][j - 1] + s1[i - 1] + s2[j - 1]);
                    }
                }
            }
        }
        return result[m][n];
    }
};

运行结果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值