• 代码随想录第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

    思想

  • 相关阅读:
    二分+ST表+递推,Cf 1237D - Balanced Playlist
    Linux openssl 搭建CA、签名证书
    Java 插入公式到PPT幻灯片
    【FPGA零基础学习之旅#15】串口接收模块设计与验证(工业环境)
    C语言常见面经详细总结
    【文本分类】《融合注意力和剪裁机制的通用文本分类模型》
    Hold the door! 基于Django建立基础框架
    三维能力是ArcGIS Pro 同时也可以进行服务发布
    flask实现python方法转换服务
    VS Code Java 3月更新|代码补全、Maven 以及 Java 插件预览版本新升级!
  • 原文地址:https://blog.csdn.net/qq_51660693/article/details/133905191