Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 712 Minimum ASCII Delete Sum for Two Strings - Walkthrough

Weighted LCS variant

Find the lowest ASCII sum of deleted characters to make strings equal. This is weighted LCS. Alternative view: find the largest ASCII sum of kept characters (the LCS), then answer = total sum - 2 * kept sum.

Trace on s1="sea"s_1 = \text{"sea"}, s2="eat"s_2 = \text{"eat"}. Common subsequence "ea" has sum 101+97=198101 + 97 = 198. Delete "s" (115) and "t" (116). Total: 231231. DP: dp[i][j]dp[i][j] = max ASCII sum of common subsequence. When chars match, add ASCII value. Time: O(mn)O(mn), Space: O(mn)O(mn).