2-D Dynamic Programming Two strings
571. Minimum ASCII Delete Sum for Two Strings
You are given two strings, s1 and s2. You may delete characters from either string until the two strings are
equal. Deleting a character costs its ASCII code: a costs 97, b costs 98, and so on up to 122 for z.
Return the smallest total cost that makes the two strings equal. Deleting every character leaves two empty strings, which are equal, so there is always an answer.
Example 1
Input: s1 = "silent", s2 = "listen"
658Explanation: Delete i, l and t from both strings, and both become "sen". The cost is 2 · (105 + 108 + 116) = 658.
Keeping "len" also leaves three letters, but it deletes s instead of l and costs 672.
Example 2
Input: s1 = "cab", s2 = "fig"
604Explanation: The strings share no letter, so every letter goes: 99 + 97 + 98 + 102 + 105 + 103 = 604.
Example 3
Input: s1 = "art", s2 = "party"
233Explanation: Delete p (112) and y (121) from "party". Nothing needs to be deleted from "art".
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ s1.length, s2.length ≤ 3000s1ands2hold only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.