Given two strings s1, s2
, find the lowest ASCII sum of deleted characters to make two strings equal.html
Example 1:python
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:this
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:spa
0 < s1.length, s2.length <= 1000
.[97, 122]
.解法:dp,dp[i][j]表示s1的前i個字符和s2的前j個字符所要刪除的字符的最小ASCII碼和。code
Python:htm
# 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:blog
# 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++:element
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++:get
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 兩個字符串的刪除操做