代码随想录算法训练营第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()];
    }
}

编辑距离总结篇

做一个总结吧

相关推荐
2501_941875283 小时前
从配置中心到动态管理的互联网工程语法演进与多语言实践分享
开发语言·python
辰烨chenye4 小时前
LeetCode Hot 100 题解 · 普通数组篇
算法·leetcode·职场和发展
hfywmsj4 小时前
广州餐饮铺位招租决策模型:多因子选址系统设计
开发语言·人工智能·python·广州餐饮铺位招租
努力的lpp6 小时前
php反序列化一(大白话版)
开发语言·web安全·php·ctf·反序列化
菜鸟~noob2336 小时前
【电子战】第03篇:CFAR(恒虚警)处理【含matlab代码】
开发语言·matlab
lhldsg6 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都6 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
泡海椒7 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
hqyjzsb7 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
辰烨chenye7 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode