[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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 两个字符串的删除操作
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签