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,dp[i][j]表示s1的前i个字符和s2的前j个字符所要删除的字符的最小ASCII码和。

Python:

# DP with rolling window
class Solution(object):
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
dp = [[0] * (len(s2)+1) for _ in xrange(2)]
for j in xrange(len(s2)):
dp[0][j+1] = dp[0][j] + ord(s2[j]) for i in xrange(len(s1)):
dp[(i+1)%2][0] = dp[i%2][0] + ord(s1[i])
for j in xrange(len(s2)):
if s1[i] == s2[j]:
dp[(i+1)%2][j+1] = dp[i%2][j]
else:
dp[(i+1)%2][j+1] = min(dp[i%2][j+1] + ord(s1[i]), \
dp[(i+1)%2][j] + ord(s2[j])) return dp[len(s1)%2][-1]

Python:

# Time:  O(m * n)
# Space: O(m * n)
class Solution2(object):
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
dp = [[0] * (len(s2)+1) for _ in xrange(len(s1)+1)]
for i in xrange(len(s1)):
dp[i+1][0] = dp[i][0] + ord(s1[i])
for j in xrange(len(s2)):
dp[0][j+1] = dp[0][j] + ord(s2[j]) for i in xrange(len(s1)):
for j in xrange(len(s2)):
if s1[i] == s2[j]:
dp[i+1][j+1] = dp[i][j]
else:
dp[i+1][j+1] = min(dp[i][j+1] + ord(s1[i]), \
dp[i+1][j] + ord(s2[j])) return dp[-1][-1]  

C++:

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

C++:

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

C++:

class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int m = s1.size(), n = s2.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + s1[i - 1];
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
int sum1 = accumulate(s1.begin(), s1.end(), 0);
int sum2 = accumulate(s2.begin(), s2.end(), 0);
return sum1 + sum2 - 2 * dp[m][n];
}
};

  

类似题目:

[LeetCode] 72. Edit Distance 编辑距离

[LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

[LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

All LeetCode Questions List 题目汇总

[LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和的更多相关文章

  1. [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. ...

  2. [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

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

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

  4. LN : leetcode 712 Minimum ASCII Delete Sum for Two Strings

    lc 712 Minimum ASCII Delete Sum for Two Strings 712 Minimum ASCII Delete Sum for Two Strings Given t ...

  5. 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. ...

  6. Java实现 LeetCode 712 两个字符串的最小ASCII删除和(最长公共子串&&ASCII值最小)

    712. 两个字符串的最小ASCII删除和 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 ...

  7. [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  8. [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. ...

  9. 【leet-code】712. 两个字符串的最小ASCII删除和

    题目描述 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 = "eat" ...

随机推荐

  1. 微信小程序~tabBar和navigator一起使用无效

    1.当注册了tabBar的时候,使用navigator时会发现不能跳转,这个时候需要在navigator上加上open-type=’switchTab’ 属性 <navigator open-t ...

  2. 微信小程序~事件绑定和冒泡

    [1]事件绑定和冒泡 事件绑定的写法同组件的属性,以 key.value 的形式. key 以bind或catch开头,然后跟上事件的类型,如bindtap.catchtouchstart.自基础库版 ...

  3. 如何使用git,进行项目的管理

    1.首先,现在git上个创建一个项目, 2.然后在本地创建一个springboot工程 3.使用git命令   git init 将这个springboot项目交给git进行管理 4.创建一个dev分 ...

  4. java项目部署到LIINUX

    天领导给个任务,把java项目部署到liunx服务器上.现记录步骤,方便以后查看.项目部署服务器步骤:服务器信息:弹性IP地址:xx.xx.xxx.xx账号:root密码:cjw@100 数据库信息: ...

  5. [Codeforces 1251F]Red-White Fence

    Description 题库链接 给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\).让你用这些木板组成一段围栏,要满足: 只用一块红木板,且所有白木板的长度均严格小于 ...

  6. 应用安全测试技术DAST、SAST、IAST对比分析【转】

    转自:https://blog.csdn.net/qq_29277155/article/details/92411079 一.全球面临软件安全危机 2010年,大型社交网站rockyou.com被曝 ...

  7. js 对嵌套页面的父页面进行跳转

    window.top.location.href = '/stores';

  8. [HTML5] Native lazy-loading for the web

    According to HTTPArchive, images are the most requested asset type for most websites and usually tak ...

  9. Redis的通用key操作

    这些操作跟具体的类型没有关系,而是跟key相关. 1.查询Redis中的key的名称: 所有key: 以my开头: 2.删除键: 3.判断某一个键是否存在: 4.重命名: 5.设置过期时间: 如果未设 ...

  10. chef test-kitchen Could not load the 'vagrant' driver from the load path 问题解决

    今天使用chef 的kitchen,运行kitchen list 发现了如下错误: >>>>>> ------Exception------- >>&g ...