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

动态规划part16

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

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

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

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

解题思路

  1. 确定dp数组以及下标的含义
    dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最少次数。
  2. 确定递推公式
  • 当word1[i - 1] 与 word2[j - 1]相同的时候 dp[i][j] = dp[i - 1][j - 1];
  • 当word1[i - 1] 与 word2[j - 1]不同时dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});
    有三种情况:
    情况一:删word1[i - 1],最少操作次数为dp[i - 1][j] + 1
    情况二:删word2[j - 1],最少操作次数为dp[i][j - 1] + 1
    情况三:同时删word1[i - 1]和word2[j - 1],操作的最少次数为dp[i - 1][j - 1] + 2
    那最后当然是取最小值
  1. dp数组初始化
    dp[i][0] = i dp[0][j] = 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数组以及下标的含义
    dp[i][j] 表示以下标i-1为结尾的字符串word1,和以下标j-1为结尾的字符串word2,最近编辑距离为dp[i][j]。
  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()];
    }
}

编辑距离总结篇

做一个总结吧

相关推荐
liulilittle2 分钟前
深度剖析:OPENPPP2 libtcpip 实现原理与架构设计
开发语言·网络·c++·tcp/ip·智能路由器·tcp·通信
88号技师8 分钟前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟14 分钟前
Java 编程之模板方法模式
java·开发语言·模板方法模式
逸风尊者36 分钟前
开发易掌握的知识:GeoHash查找附近空闲车辆
java·后端
ゞ 正在缓冲99%…36 分钟前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
碎叶城李白1 小时前
若依学习笔记1-validated
java·笔记·学习·validated
上单带刀不带妹1 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
Kaltistss1 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
都叫我大帅哥1 小时前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥1 小时前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis