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. 使用RNN解决句子对匹配问题的常见网络结构

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 除了序列标注问题外,句子对匹配(Sentence Pair Matching)问题也是NLP中非经常见 ...

  2. java重载中的基本类型的自动类型转换

    当传递到函数的参数的数据类型表示的范围小于函数形参的参数类型遵循如下原则 : char类型比较特殊, 直接转换为int:  char ->int ->long->float-> ...

  3. 解决Struts配置文件里无提示信息的问题

    (1)在struts2配置文件编写的时候.有可能无法提示所有信息,在配置文件里打个"<" 后,并没有不论什么的提示信息(使用快捷键Alt+/ 也不提示) 原因是下边的  &q ...

  4. Xcode6 设置LaunchImage图标

    最近设置LaunchImage图标时发现怎么都没有效果,后来发现是Xcode6中新建项目的时候会默认添加一个LaunchScreen.xib的文件,我们启动程序的时候也会发现,加载的时LaunchSc ...

  5. 使用buildroot搭建linux文件系统【转】

    本文转载自:http://blog.csdn.net/metalseed/article/details/45423061 (文件系统搭建,强烈建议直接用buildroot,官网上有使用教程非常详细b ...

  6. Ubuntu18开启redis服务自启动

    设置redis服务开机自启动. 1.创建配置文件夹 sudo mkdir /etc/redis sudo cp /usr/local/redis/redis.conf /etc/redis sudo ...

  7. 【转】浏览器中输入url后发生了什么

    原文地址:http://www.jianshu.com/p/c1dfc6caa520 在学习前端的过程中经常看到这样一个问题:当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,供个人复 ...

  8. snnu1111(子序列求和)

    1111: 子序列求和 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 2[Submit][Status][Web Board] [ ...

  9. 用jQuery插件来提升SharePoint列表表单用户体验

    本文将描述如何通过简单的CSS和jQuery插件提升SharePoint默认的列表表单体验.这些小技巧并不仅仅改善了外观,还提升了可用性. 剩余字数 我们以通知列表为例.通知正文字段假设要求不应该超过 ...

  10. 使用merge-using语句初始化数据库

    创建三张表Student.Course.Enrollment CREATE TABLE [dbo].[Student] ( [StudentID] INT IDENTITY (1, 1) NOT NU ...