• 代码随想录第46天 | ● 583. 两个字符串的删除操作 ● 72. 编辑距离


    583. 两个字符串的删除操作

    /**
     * @param {string} word1
     * @param {string} word2
     * @return {number}
     */
    var minDistance = function(word1, word2) {
          const dp = new Array(word1.length + 1).fill(0).map(x => new Array(word2.length + 1).fill(0));
            for (let i= 0; i <= word1.length;i++) dp[i][0] = i;
            for (let j = 0; j <= word2.length; j++) dp[0][j] = j;
            for (let i = 1; i <= word1.length; i++) {
                for (let j = 1; j <= word2.length; j++) {
                    if (word1[i - 1] == word2[j - 1]) {
                        dp[i][j] = dp[i - 1][j - 1];
                    } else {
                        dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
                    }
                }
            }
            return dp[word1.length][word2.length];
        }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    思想

    • dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最少次数。
    • 递推公式:dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
      在这里插入图片描述
      在这里插入图片描述
    • 初始化
      dp[i][0] 和 dp[0][j]是一定要初始化的。

    dp[i][0]:word2为空字符串,以i-1为结尾的字符串word1要删除多少个元素,才能和word2相同呢,很明显dp[i][0] = i。

    在这里插入图片描述

    也可以
    本题和动态规划:1143.最长公共子序列 (opens new window)基本相同,只要求出两个字符串的最长公共子序列长度即可,那么除了最长公共子序列之外的字符都是必须删除的,最后用两个字符串的总长度减去两个最长公共子序列的长度就是删除的最少步数。

    class Solution {
    public:
        int minDistance(string word1, string word2) {
            vector<vector<int>> dp(word1.size()+1, vector<int>(word2.size()+1, 0));
            for (int i=1; i<=word1.size(); i++){
                for (int j=1; j<=word2.size(); j++){
                    if (word1[i-1] == word2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
                    else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
            return word1.size()+word2.size()-dp[word1.size()][word2.size()]*2;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    72. 编辑距离

    /**
     * @param {string} word1
     * @param {string} word2
     * @return {number}
     */
    var minDistance = function(word1, word2) {
         let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));
    
        for(let i = 1; i <= word1.length; i++) {
            dp[i][0] = i; 
        }
    
        for(let j = 1; j <= word2.length; j++) {
            dp[0][j] = j;
        }
    
        for(let i = 1; i <= word1.length; i++) {
            for(let j = 1; j <= word2.length; j++) {
                if(word1[i-1] === word2[j-1]) {
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 1);
                }
            }
        }
        return dp[word1.length][word2.length];
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    思想

  • 相关阅读:
    空气质量预报模式系统(CMAQ)
    C# 中,使用 LINQ 示例 备忘
    测试经验总结
    Shell 编程实践
    分布式一致性协议 之 Lease机制
    【元宇宙】治理元宇宙,让它成为理想的未来之地
    RL-D1电流继电器
    HCIP自我重修总笔记
    shadowDom
    STM32CubeIDE链接脚本讲解
  • 原文地址:https://blog.csdn.net/qq_51660693/article/details/133905191