[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 character
b) Delete a character
c) Replace a character

思路

编辑距离是动态规划最典型的练习题之一。如果我们考察word1[0, i]到word2[0, j]的编辑距离,根据对最后一个字符的处理,可以有如下两种情况:

1)如果word1[i] == word2[j],那么不用处理最后一个字符,其最优编辑过程也就是从word1[0, i-1]到word2[0, j-1]的编辑过程。此时dp[i][j] = dp[i-1][j-1]。

2)如果word1[i-1] != word2[j-1],那么根据对最后一个字符的处理分类,有如下三种可能:

a、替换:此时对应的编辑过程就是:step 1: 将word1[0, i-1]编辑为word2[0, j-1];step 2: 将word1[i]替换为word2[j]。此时dp[i][j] = dp[i-1][j-1] + 1。

b、插入:此时对应的编辑过程就是:step 1: 将word1[0, i]编辑为word2[0, j-1];step 2: 在word2[0, j-1]的末尾加上words[j]。此时dp[i][j] = dp[i][j-1] + 1。

c、删除:此时对应的编辑过程就是:step 1: 将word1[0, i-1]编辑为word2[0, j];step 2: 将word1[i]删除。此时dp[i][j] = dp[i-1][j] + 1。

注意在上述三种情况中,step 1和step 2的执行次序可以调换。

分析可知,dp[i][j]依然只和它左方、上方以及左上方的三个元素有关,因此空间复杂度可以采用动态规划的方式优化到O(len2),其中len1和len2分别是字符串word1和word2的长度。如果对空间复杂度比较在意,还可以进一步将空间复杂度优化为O(min(len1, len2)) (适合len2远大于len1的情况,此时虽然理论上程序的时间复杂度仍然为O(len1 * len2),但是运行时间却会增加不少,请思考一下为什么?O(∩_∩)O)。

代码

1、O(len1 * len2)空间复杂度的DP解法:

class Solution {
public:
    int minDistance(string word1, string word2) 
    {
        int len1 = word1.length(), len2 = word2.length();
        vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
        for(int i = 0; i <= len1; ++i)  // initialize the first column
            dp[i][0] = i;
        for(int i = 0; i <= len2; ++i)  // initialize the first row
            dp[0][i] = i;
        for(int i = 1; i <= len1; ++i)
        {
            for(int j = 1; j <= len2; ++j)
            {
                if(word1[i-1] == word2[j-1])
                    dp[i][j] = dp[i-1][j-1];
                else
                    dp[i][j] = min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1;
            }
        }
        return dp[len1][len2];
    }
};

2、O(min(len1, len2))空间复杂度的DP解法:

class Solution {
public:
    int minDistance(string word1, string word2) 
    {
        if(word1.length() < word2.length()) {   
            swap(word1, word2); // optional: may decrease space complexity while increasing running time
        }
        int len1 = word1.length(), len2 = word2.length();
        vector<vector<int>> dp(2, vector<int>(len2 + 1, 0));
        for(int i = 0; i <= len2; ++i)  // initialize the first column
            dp[0][i] = i;
        for(int i = 1; i <= len1; ++i)
        {
            dp[i%2][0] = i;
            for(int j = 1; j <= len2; ++j)
            {
                if(word1[i-1] == word2[j-1])
                    dp[i%2][j] = dp[(i-1)%2][j-1];
                else
                    dp[i%2][j] = min(min(dp[i%2][j-1], dp[(i-1)%2][j]), dp[(i-1)%2][j-1]) + 1;
            }
        }
        return dp[len1%2][len2];
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值