代码随想录算法训练营第59天(动态规划16 ● 583. 两个字符串的删除操作 ● 72. 编辑距离 ● 编辑距离总结篇

动态规划part16

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

本题和动态规划:115.不同的子序列 相比,其实就是两个字符串都可以删除了,情况虽说复杂一些,但整体思路是不变的。

题目链接:583. 两个字符串的删除操作

文章/视频讲解:583. 两个字符串的删除操作

解题思路

  1. 确定dp数组以及下标的含义
    dpij:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最少次数。
  2. 确定递推公式
  • 当word1i - 1 与 word2j - 1相同的时候 dpij = dpi - 1j - 1;
  • 当word1i - 1 与 word2j - 1不同时dpij = min({dpi - 1j - 1 + 2, dpi - 1j + 1, dpij - 1 + 1});
    有三种情况:
    情况一:删word1i - 1,最少操作次数为dpi - 1j + 1
    情况二:删word2j - 1,最少操作次数为dpij - 1 + 1
    情况三:同时删word1i - 1和word2j - 1,操作的最少次数为dpi - 1j - 1 + 2
    那最后当然是取最小值
  1. dp数组初始化
    dpi0 = i dp0j = j;
  2. 确定遍历顺序
  3. 举例推导dp数组
java 复制代码
// 思路1
class Solution {
    public int minDistance(String word1, String word2) {
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];

        for (int i = 0; i < word1.length() + 1; i++) dp[i][0] = i;
        for (int j = 0; j < word2.length() + 1; j++) dp[0][j] = j;

        for(int i = 1; i < word1.length() + 1; i++){            
            for(int j = 1; j < word2.length() + 1; j++){             
                if(word1.charAt(i - 1) == word2.charAt(j - 1)){
                    dp[i][j] = dp[i - 1][j - 1];
                }else{
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 2);
                }
            }
        }
        return dp[word1.length()][word2.length()];

    }
}


// 思路2:视频讲解中最后提到的另一种那个思路 在1143.最长公共子序列 中的返回值return 中稍作修改
class Solution {
    public int minDistance(String word1, String word2) {
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];

        for(int i = 1; i < word1.length() + 1; i++){
            
            for(int j = 1; j < word2.length() + 1; j++){
              
                if(word1.charAt(i - 1) == word2.charAt(j - 1)){
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }else{
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return word1.length() + word2.length() - 2 * dp[word1.length()][word2.length()];

    }
}

72. 编辑距离

最终我们迎来了编辑距离这道题目,之前安排题目都是为了 编辑距离做铺垫。

题目链接:72. 编辑距离

文章/视频讲解:72. 编辑距离

解题思路

  1. 确定dp数组以及下标的含义
    dpij 表示以下标i-1为结尾的字符串word1,和以下标j-1为结尾的字符串word2,最近编辑距离为dpij
  2. 递推公式
java 复制代码
if (word1[i - 1] == word2[j - 1])
    不操作
if (word1[i - 1] != word2[j - 1])
    删  word1删除一个元素
    增  word1增加元素,即word2删除元素
    换
java 复制代码
class Solution {
    public int minDistance(String word1, String word2) {
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];

        for(int i = 0; i < word1.length() + 1; i++) dp[i][0] = i;
        for(int j = 0; j < word2.length() + 1; j++) dp[0][j] = j;

        for(int i = 1; i < word1.length() + 1; i++){
            for(int j = 1; j < word2.length() + 1; j++){
                if(word1.charAt(i - 1) == word2.charAt(j - 1)){
                    dp[i][j] = dp[i - 1][j - 1];
                }else{
                    dp[i][j] = Math.min(Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1), dp[i - 1][j - 1] + 1);
                }
            }
        }
        return dp[word1.length()][word2.length()];
    }
}

编辑距离总结篇

做一个总结吧

相关推荐
黄贵根3 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll11116 小时前
Claude Code离线安装方案揭秘
开发语言·php
wabs6666 小时前
关于图论【卡码网117.软件构建的思考】
数据结构·算法·软件构建·图论·卡码网
mifengxing7 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
wling03017 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴7 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
Tongzhi20267 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
Wang's Blog8 小时前
AI Agent白手起家30: 动态示例选择器之根据长度选择 Few Shot 示例
算法
xrandzj8 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_9 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯