• 每日一题 712两个字符串的最小ASCLL删除和


    题目

    题目
    给定两个字符串s1 和 s2,返回 使两个字符串相等所需删除字符的 ASCII 值的最小和

    示例 1:

    输入: s1 = “sea”, s2 = “eat”
    输出: 231
    解释: 在 “sea” 中删除 “s” 并将 “s” 的值(115)加入总和。
    在 “eat” 中删除 “t” 并将 116 加入总和。
    结束时,两个字符串相等,115 + 116 = 231 就是符合条件的最小和。
    示例 2:

    输入: s1 = “delete”, s2 = “leet”
    输出: 403
    解释: 在 “delete” 中删除 “dee” 字符串变成 “let”,
    将 100[d]+101[e]+101[e] 加入总和。在 “leet” 中删除 “e” 将 101[e] 加入总和。
    结束时,两个字符串都等于 “let”,结果即为 100+101+101+101 = 403 。
    如果改为将两个字符串转换为 “lee” 或 “eet”,我们会得到 433 或 417 的结果,比答案更大。

    提示:

    0 <= s1.length, s2.length <= 1000
    s1 和 s2 由小写英文字母组成

    题解

    记忆化搜索

    class Solution {
        private char[] s,t;
        private int[][] cache;
    
        public int minimumDeleteSum(String s1, String s2) {
            s = s1.toCharArray();
            t = s2.toCharArray();
            int n = s.length, m = t.length;
            cache = new int[n][m];
            for (int i = 0; i < n; i++) {
                Arrays.fill(cache[i],-1);
            }
            return dfs(n - 1, m - 1);
        }
    
        public int dfs(int i, int j) {
            if (i < 0) {
                int ans = 0;
                while (j >= 0) {
                    ans += t[j--];
                }
                return ans;
            }
            if (j < 0) {
                int ans = 0;
                while (i >= 0) {
                    ans += s[i--];
                }
                return ans;
            }
            if (cache[i][j] != -1) {
                return cache[i][j];
            }
            if (s[i] == t[j]) {
                return cache[i][j] = dfs(i - 1, j - 1);
            }
            return cache[i][j] = Math.min(dfs(i, j - 1) + (int) t[j], dfs(i - 1, j) + (int) s[i]);
        }
    }
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    递推

    class Solution {
        public int minimumDeleteSum(String s1, String s2) {
            char[] s = s1.toCharArray();
            char[] t = s2.toCharArray();
            int n = s.length, m = t.length;
            int[][] f = new int[n + 1][m + 1];
            for (int j = 0; j < m; j++) {
                f[0][j + 1] = (int) t[j] + f[0][j];
            }
            for (int i = 0; i < n; i++) {
                f[i + 1][0] = (int) s[i] + f[i][0];
                for (int j = 0; j < m; j++) {
                    f[i + 1][j + 1] = s[i] == t[j] ? f[i][j] :
                            Math.min(f[i][j + 1] + s[i], f[i + 1][j] + t[j]);
                }
            }
            return f[n][m];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    空间优化

    class Solution {
        public int minimumDeleteSum(String s1, String s2) {
            char[] t = s2.toCharArray();
            int m = t.length;
            int[] f = new int[m + 1];
            for (int j = 0; j < m; j++) {
                f[j + 1] = (int) t[j] + f[j];
            }
            for (char x : s1.toCharArray()) {
                int pre = f[0];
                f[0] += (int) x;
                for (int j = 0; j < m; j++) {
                    int temp = f[j + 1];
                    f[j + 1] = x == t[j] ? pre : Math.min(f[j + 1] + x, f[j] + t[j]);
                    pre = temp;
                }
            }
            return f[m];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    华为配置Mesh普通业务示例
    线段树 模板 Java语言版
    Exch:移动或清理 Exchange Server 日志文件
    SCI投稿经验(三) 回复审稿人
    Android开发系列(十二)Jetpack Compose之BottomSheet
    使用boost::geometry::union_ 合并边界(内、外):方案二
    redis缓存一致性讨论
    自定义注解(切面实现)
    java计算机毕业设计工厂生产计划与进度管理系统源码+mysql数据库+系统+lw文档+部署
    AWS 中文入门开发教学 9- 网络IP范围设计
  • 原文地址:https://blog.csdn.net/fffffall/article/details/133738097