/**
* Problem: LeetCode 712 - Minimum ASCII Delete Sum for Two Strings
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minimumDeleteSum(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
// Base cases: cost of deleting all characters from s1 or s2
for (int i = 1; i <= m; i++) {
dp[i][0] = dp[i - 1][0] + s1.charAt(i - 1);
}
for (int j = 1; j <= n; j++) {
dp[0][j] = dp[0][j - 1] + s2.charAt(j - 1);
}
// DP state transitions
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(
dp[i - 1][j] + s1.charAt(i - 1),
dp[i][j - 1] + s2.charAt(j - 1)
);
}
}
}
return dp[m][n];
}
}
"""
Problem: LeetCode 712 - Minimum ASCII Delete Sum for Two Strings
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def minimumDeleteSum(self, s1: str, s2: str) -> int:
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Base cases: cost of deleting all characters from s1 or s2
for i in range(1, m + 1):
dp[i][0] = dp[i - 1][0] + ord(s1[i - 1])
for j in range(1, n + 1):
dp[0][j] = dp[0][j - 1] + ord(s2[j - 1])
# DP state transitions
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(
dp[i - 1][j] + ord(s1[i - 1]),
dp[i][j - 1] + ord(s2[j - 1])
)
return dp[m][n]
/**
* Problem: LeetCode 712 - Minimum ASCII Delete Sum for Two Strings
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
#include <algorithm>
class Solution {
public:
int minimumDeleteSum(std::string s1, std::string s2) {
int m = s1.length();
int n = s2.length();
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));
// Base cases: cost of deleting all characters from s1 or s2
for (int i = 1; i <= m; ++i) {
dp[i][0] = dp[i - 1][0] + static_cast<int>(s1[i - 1]);
}
for (int j = 1; j <= n; ++j) {
dp[0][j] = dp[0][j - 1] + static_cast<int>(s2[j - 1]);
}
// DP state transitions
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];
} else {
dp[i][j] = std::min(
dp[i - 1][j] + static_cast<int>(s1[i - 1]),
dp[i][j - 1] + static_cast<int>(s2[j - 1])
);
}
}
}
return dp[m][n];
}
};
/**
* Problem: LeetCode 712 - Minimum ASCII Delete Sum for Two Strings
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {string} s1
* @param {string} s2
* @return {number}
*/
var minimumDeleteSum = function(s1, s2) {
const m = s1.length;
const n = s2.length;
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
// Base cases: cost of deleting all characters from s1 or s2
for (let i = 1; i <= m; i++) {
dp[i][0] = dp[i - 1][0] + s1.charCodeAt(i - 1);
}
for (let j = 1; j <= n; j++) {
dp[0][j] = dp[0][j - 1] + s2.charCodeAt(j - 1);
}
// DP state transitions
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (s1.charAt(i - 1) === s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(
dp[i - 1][j] + s1.charCodeAt(i - 1),
dp[i][j - 1] + s2.charCodeAt(j - 1)
);
}
}
}
return dp[m][n];
};