[LeetCode] 72. Edit Distance

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 = “horse”, word2 = “ros”
Output: 3
Explanation:
horse -> rorse (replace ‘h’ with ‘r’)
rorse -> rose (remove ‘r’)
rose -> ros (remove ‘e’)

Example 2:

Input: word1 = “intention”, word2 = “execution”
Output: 5
Explanation:
intention -> inention (remove ‘t’)
inention -> enention (replace ‘i’ with ‘e’)
enention -> exention (replace ‘n’ with ‘x’)
exention -> exection (replace ‘n’ with ‘c’)
exection -> execution (insert ‘u’)

解法1:递归

分别比较三种操作的操作数
当word1[i]和word2[j]相同时,直接比较下一位word1[i+1]和word2[j+1]
当word1[i]和word2[j]不同时:
1.插入操作,直接插入word2[j],跳过word2[j],比较word1[i]和word2[j+1];
2.删除操作,将word1[i]直接删掉,比较word1[i+1]和word2[j];
3.替换操作,将word1[i]修改为word2[j],比较word1[i+1]和word2[j+1];
用记忆数组保存计算状态,insertCount,removeCount和replaceCount表示当前对应位置进行三种操作。

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m=word1.size(), n = word2.size();
        vector<vector<int> > memory(m, vector<int>(n));
        return helper(word1, 0, word2, 0, memory);
    }
    
    int helper(string word1, int i, string word2, int j, vector<vector<int> >& memory){
        if(i == word1.size()) return word2.size()-j;
        if(j == word2.size()) return word1.size()-i;
        if(memory[i][j] > 0) return memory[i][j];
        int res = 0;
        if(word1[i] == word2[j])
            return helper(word1,i+1,word2,j+1,memory);
        else{
            int insertCount = helper(word1,i,word2,j+1,memory);
            int removeCount = helper(word1,i+1,word2,j,memory);
            int replaceCount = helper(word1,i+1,word2,j+1,memory);
            res = min(insertCount,min(removeCount, replaceCount)) + 1;
        }
        memory[i][j] = res;
        return memory[i][j];
    }
};

解法2:动态规划

用一个dp二维数组来表示word1前i个字符和word2前j个字符匹配的编辑距离
当word1[i]==word2[j]时,dp[i][j] = dp[i-1][j-1];
当word1[i] !=word2[j]时,dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1;

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

参考

https://www.cnblogs.com/grandyang/p/4344107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值