lc 712 Minimum ASCII Delete Sum for Two Strings


712 Minimum ASCII Delete Sum for Two Strings

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.

Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

Note:

  • 0 < s1.length, s2.length <= 1000.

  • All elements of each string will have an ASCII value in [97, 122].

DP Accepted

dp[i][j]表示使s1.substr(0, i)、s2.substr(0, j)相等的最小代价,不包括s1[i]和s2[j]。

dp[0][0] = 0;

如果s1[i-1] = s2[j-1],那么就不用增加代价。dp[i][j] = dp[i-1][j-1];

否则,删除s1[i-1]或s2[j-1]。dp[i][j] = min(dp[i-1][j]+s1[i-1], dp[i][j-1]+s2[j-1]);

class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int m = s1.size(), n = s2.size();
int dp[m+1][n+1] = {0};
for (int i = 1; i < n+1; i++) dp[0][i] = dp[0][i-1]+(s2[i-1]);
for (int i = 1; i < m+1; i++) {
dp[i][0] = dp[i-1][0]+s1[i-1];
for (int j = 1; j < n+1; j++) {
if (s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1];
else dp[i][j] = min(dp[i-1][j]+s1[i-1], dp[i][j-1]+s2[j-1]);
}
}
return dp[m][n];
}
};

LN : leetcode 712 Minimum ASCII Delete Sum for Two Strings的更多相关文章

  1. [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  2. LeetCode 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  3. LC 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  4. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  6. 712. Minimum ASCII Delete Sum for Two Strings

    题目: Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings eq ...

  7. Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings)

    Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings) 给定两个字符串s1, s2,找到 ...

  8. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  9. [Swift]LeetCode712. 两个字符串的最小ASCII删除和 | Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

随机推荐

  1. MongoDB 操作手冊CRUD 更新 update

    改动记录 概述 MongoDB提供了update()方法用于更新记录. 这种方法接受下面參数:     一个更新条件的JSON对象用于匹配记录,一个更新操作JSON对象用于声明更新操作,和一个选项JS ...

  2. POJ3761 Bubble Sort

    对1~n组成的序列进行冒泡排序,一共进行了k趟,问有几个符合题意的序列. 注意:这里指每一趟是指交换当前相邻的全部逆序对,比如:2 1 4 3进行一趟交换就是1 2 3 4 假设我们细心观察.就会发现 ...

  3. vs2010 创建和发布 webservice

    1 打开VS2010,菜单    文件->新建->项目 2 选择[ASP.net 空web应用程序],将其命名为自己想的工程名称. 3 右键点击工程,添加->新建项 选择 web服务 ...

  4. Mac mysql 运行sql文件中文乱码的问题

    别再傻傻的改什么mysql的编码格式了. 是.sql文件的编码有问题,把sql文件的编码格式改成utf-8就行了. mac怎么修改呢? vscode最爽了. 用vscode打开.sql文件,然后点右下 ...

  5. BZOJ_3058_四叶草魔杖_kruscal+状压DP

    BZOJ_3058_四叶草魔杖_kruscal+状压DP Description 魔杖护法Freda融合了四件武器,于是魔杖顶端缓缓地生出了一棵四叶草,四片叶子幻发着淡淡的七色光.圣剑护法rainbo ...

  6. 提高你的Python: 解释‘yield’和‘Generators(生成器)’

    在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" 或者 "定义和使用函数" ...

  7. ava Double: 四舍五入并设置小数点位数

    public static void main(String[] args) { // 1. 先乘后四舍五入, 再除; double d = 62.31060027198647; double d2 ...

  8. MySQL中怎么查询一张表的列数

    select count(1) from information_schema.columns where table_schema='dbname' and table_name='tbname;

  9. asp.net mvc5 使用百度ueditor 本编辑器完整示例(上)

    最近做一个项目,用到了百度ueditor富文本编辑器,功能强大,在线编辑文档,上传图片\视频.附件. MVC 模型的控制器准备: 1.建立模型. 在项目中Model 文件夹中建立 文章 模型,注意如果 ...

  10. ubuntu16.04下使用python3开发时,安装pip3与scrapy,升级pip3

    1)安装pip3: sudo apt-get install python3-pip 2)安装scrapy sudo pip3 install scrapy 若出现版本过低问题: pip3 insta ...