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. C#&.NET高级面试题

    转自http://chaoyouzhuo.blog.163.com/blog/static/1263760012011109114131316/ 1. DateTime.Parse(myString) ...

  2. Linux pipe 源代码分析

    Linux pipe 源代码分析      管道pipe作为Unix中历史最悠久的IPC机制,存在各个版本号的Unix中,主要用于父子进程之间的通信(使用fork,从而子进程会获得父进程的打开文件表) ...

  3. MySql InnoDb还原工具

    通过任意文件下载找到了mysql的备份,表类型是独享式innodb,由一个frm文件和一个ibd文件组成. 本以为直接复制到本地的mysql数据目录中即可恢复数据,但在查询时却发现并不如所愿: mys ...

  4. JavaScript基础教程复习笔记

    document.write("<h1>这是一个标题</h1>"); 您只能在 HTML 输出中使用 document.write.如果您在文档加载后使用该 ...

  5. Linux 下的编辑/编译器

    linux 首先有两个重量级的文本编辑器:vim 和 emacs 此外有如下三种比较好的开放环境: 1.Anjuta Anjuta DevStudio 的官方地址:http://anjuta.sour ...

  6. RFC函数设置外部断点

  7. (1)iOS9完美越狱

    方式一:同步推越狱,其实用的也是方式二 参考:iOS9.3.5不完美越狱(点击跳转) 方式二:使用impactor越狱. 下载地址:http://www.pc6.com/mac/505285.html

  8. YTU 2811: 打鱼还是晒网

    2811: 打鱼还是晒网 时间限制: 1 Sec  内存限制: 128 MB 提交: 192  解决: 150 题目描述 中国有句俗话"三天打鱼,两天晒网".小王从2000年的1月 ...

  9. bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片

    files=(a b c d e f g h i j k l m n o p)cnt="${#files[@]}"let cnt1="($cnt+2)/3"le ...

  10. I.MX6 简单电路模拟USB设备的插入

    /**************************************************************************** * I.MX6 简单电路模拟USB设备的插入 ...