Minimum ASCII Delete Sum for Two Strings
Description
Given two strings s1, s2
, find the lowest ASCII sum of deleted characters to make two strings equal.
Example 1:
Example 2:
Note:
0 < s1.length, s2.length <= 1000
.All elements of each string will have an ASCII value in
[97, 122]
.
Solution
Let dp[i + 1][j + 1]
be the minimum delete sum of s1[0..i]
and s2[0..j]
.
If
s1[i] == s2[j]
, thendp[i + 1][j + 1] = dp[i][j]
Otherwise, delete either
s1[i]
ors2[j]
or both. So,dp[i + 1][j + 1] = min(dp[i][j + 1] + s1[i], dp[i + 1][j] + s2[j])
Both
dp[i][j + 1]
anddp[i][j + 1]
contain the case where boths1[i]
ands2[j]
are deleted.
Last updated