给定两个字符串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:
def minimumDeleteSum(self, s1: str, s2: str) -> int:
m, n = len(s1), len(s2)
f = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
f[i][0] = f[i - 1][0] + ord(s1[i - 1])
for j in range(1, n + 1):
f[0][j] = f[0][j - 1] + ord(s2[j - 1])
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
f[i][j] = f[i - 1][j - 1]
else:
f[i][j] = min(
f[i - 1][j] + ord(s1[i - 1]), f[i][j - 1] + ord(s2[j - 1])
)
return f[m][n]
class Solution {
public int minimumDeleteSum(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1.charAt(i - 1);
}
for (int j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2.charAt(j - 1);
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j]
= Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
}
}
}
return f[m][n];
}
}
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int m = s1.size(), n = s2.size();
int f[m + 1][n + 1];
memset(f, 0, sizeof f);
for (int i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1[i - 1];
}
for (int j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2[j - 1];
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1[i - 1] == s2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = min(f[i - 1][j] + s1[i - 1], f[i][j - 1] + s2[j - 1]);
}
}
}
return f[m][n];
}
};
func minimumDeleteSum(s1 string, s2 string) int {
m, n := len(s1), len(s2)
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for i, c := range s1 {
f[i+1][0] = f[i][0] + int(c)
}
for j, c := range s2 {
f[0][j+1] = f[0][j] + int(c)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if s1[i-1] == s2[j-1] {
f[i][j] = f[i-1][j-1]
} else {
f[i][j] = min(f[i-1][j]+int(s1[i-1]), f[i][j-1]+int(s2[j-1]))
}
}
}
return f[m][n]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
function minimumDeleteSum(s1: string, s2: string): number {
const m = s1.length;
const n = s2.length;
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1[i - 1].charCodeAt(0);
}
for (let j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2[j - 1].charCodeAt(0);
}
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
if (s1[i - 1] === s2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = Math.min(
f[i - 1][j] + s1[i - 1].charCodeAt(0),
f[i][j - 1] + s2[j - 1].charCodeAt(0),
);
}
}
}
return f[m][n];
}
/**
* @param {string} s1
* @param {string} s2
* @return {number}
*/
var minimumDeleteSum = function (s1, s2) {
const m = s1.length;
const n = s2.length;
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
f[i][0] = f[i - 1][0] + s1[i - 1].charCodeAt(0);
}
for (let j = 1; j <= n; ++j) {
f[0][j] = f[0][j - 1] + s2[j - 1].charCodeAt(0);
}
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
if (s1[i - 1] === s2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = Math.min(
f[i - 1][j] + s1[i - 1].charCodeAt(0),
f[i][j - 1] + s2[j - 1].charCodeAt(0),
);
}
}
}
return f[m][n];
};