【代码训练营】day55 | 583. 两个字符串的删除操作 & 72. 编辑距离

文章介绍了如何使用Java编程解决LeetCode上的两个字符串的删除操作问题,通过动态规划求解最小编辑距离。方法包括初始化矩阵、递推公式和状态转移,以及对两种不同情况的处理:相等字符和不相等字符。此外,还提到了一种利用最长公共子序列优化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所用代码 java

两个字符串的删除操作 583

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

思路

  • dp[i] [j]:以i-1为结尾的word1 和 以j-1为结尾的word2为结尾 为了让两个字符串相同最少操作次数为dp[i] [j]

  • 递推公式:

    • 相同 if(word1[i-1] == word2[j-1]) dp[i][j] = dp[i-1][j-1]

    • 不相同 dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 2)

      • 删word1的字母,相当于删一次 dp[i-1][j] + 1
      • 删word2的字母,删一次 dp[i][j-1] + 1
      • 两个字母都删,删两次 dp[i-1][j-1] + 2 其实就相当于先删word2 dp[i][j] = dp[i][j-1] + 1,然后再删word1 dp[i][j-1] = dp[i-1][j-1] + 1
  • 初始化:

    • dp[0][j] = j:相当于word1为空字符串,所以要删j(word2.length)个元素
    • dp[i][0] = i:相当于word2为空字符串,所以要删i(word1.length)个元素
    • dp[0][0] = 0:空字符与空字符匹配
  • 遍历方向

  • 打印

class Solution {
    public int minDistance(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        int[][] dp = new int[n1+1][n2+1];
        // 初始化
        // dp[i][0],相当于word2为空串,就相当于word1有几个字母就要删几个
        for (int i = 0; i <= n1; i++) {
            dp[i][0] = i;
        }
        // dp[0][j],相当于word1为空串,就相当于word2有几个字母就要删几个
        for (int j = 0; j <= n2; j++) {
            dp[0][j] = j;
        }
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                // 相等就不用删除,和i-1,j-1一样
                if (word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else {
                    // 不相等就需要把word1删一个,或者word2删一个
                    // 或者两个都要删一个,就相当于其中一个先删除一个字母,然后再删除一个字母
                    dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1);
                }
            }
//            System.out.println(Arrays.toString(dp[i]));
        }
        return dp[n1][n2];
    }
}

总结

打印结果:

Finished:
    Your input:"leetcode"
            "etco"
    Output:4
    Expected:4
    stdout:
        0   1  2  3  4
        [1, 2, 3, 4, 5]
        [2, 1, 2, 3, 4]
        [3, 2, 3, 4, 5]
        [4, 3, 2, 3, 4]
        [5, 4, 3, 2, 3]
        [6, 5, 4, 3, 2]
        [7, 6, 5, 4, 3]
        [8, 7, 6, 5, 4]

本题还有一种方法,因为他和之前的最长公共子序列类似,我们第一步找到最长公共子序列,这个子序列就是两者需要保留的最大长度,然后用两者的len减最长公共子序列就是需要删除的少步骤。

class Solution {
    public int minDistance(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        int[][] dp = new int[n1+1][n2+1];
        for (int i = n1; i > 0; i--) {
            for (int j = n2; j > 0; 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]);
                }
            }
//            System.out.println(Arrays.toString(dp[i]));
        }
        return word1.length() - dp[n1][n2] + word2.length() - dp[n1][n2];
    }
}

遍历距离 LeetCode 72

题目链接:遍历距离 LeetCode 72 - 困难

思路

  • dp[i] [j]:以i-1为结尾的word1和以j-1为结尾的word2,最少的操作次数为dp[i] [j]

  • 递推公式:

    • 相同,不需要操作 if(word[i-1] == word[j-1]) dp[i][j] = dp[i-1][j-1]

    • 不同

      • 增:删除和添加是一样的,相当于逆向操作另一个数
      • 删:可以删word1或者word2 min(dp[i-1][j] + 1, dp[i][j-1] + 1)
      • 改:相同就是去上一个元素不操作,不同就是在取上一个元素的基础上再多加一个1 dp[i][j] = dp[i-1][j-1] + 1
  • 初始化:

    • dp[i][0]=i word2空串需操作i次,就是删i个字符
    • dp[0][j]=j word1空串需操作j次,就是删j个字符
  • 遍历顺序

  • 打印dp

class Solution {
    public int minDistance(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        int[][] dp = new int[n1+1][n2+1];
        // 初始化
        // dp[i][0],相当于word2为空串,所以word1有几个字母就得删几个(word2添加几个)
        for (int i = 0; i <= n1; i++) {
            dp[i][0] = i;
        }
        // dp[0][j],相当于word1为空串,word2有几个字母就要删几个(或者word1添加几个)
        for (int j = 0; j <= n2; j++) {
            dp[0][j] = j;
        }
//        System.out.println(Arrays.toString(dp[0]));for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (word1.charAt(i-1) == word2.charAt(j-1)){
                    // 相等的情况就不操作,取上一次相等时候的值
                    dp[i][j] = dp[i-1][j-1];
                }else {
                    // 不相等主要有两个操作,一是删(word1和word2都可以删,增和删一样里面那个min),
                    // 二是改dp[i-1][j-1] + 1
                    dp[i][j] = Math.min(Math.min(dp[i-1][j] + 1, dp[i][j-1]+ 1), dp[i-1][j-1] + 1);
                }
            }
//            System.out.println(Arrays.toString(dp[i]));
        }
        return dp[n1][n2];
    }
}

总结

打印结果:第一行为word1为空的情况,即word2有几个元素就需删几个;第一列为word1为空的情况,即word2有几个元素就要删除几个元素。

    Your input:"horse"
            "ros"
    Output:3
    Expected:3
    stdout:
        [0, 1, 2, 3]
        [1, 1, 2, 3]
        [2, 2, 1, 2]
        [3, 2, 2, 2]
        [4, 3, 3, 2]
        [5, 4, 4, 3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值