Given two strings, return the lowest ASCII sum of the deleted characters to make the strings equal.
Returning the lowest ASCII sum of deleted characters to make two strings equal is equivalent to finding the maximum ASCII of a common subsequence. This problem is a variant of the Longest Common Subsequence (LCS).
Instead of looking for the length of the longest common subsequence, we'll look for the maximum ASCII sum of a common subsequence. This solves situations like shown in the example test case, when the string "eet" is chosen over "let".
Altering LCS for ASCII sum requires only one change in the recursive function. In the recurrence relation, when the characters of the two strings are equal, rather than adding 1 for a path length, we add the ASCII total to the answer
Then in the main function, we add a few lines for ASCII sum calculations
We find the ASCII totals for each string
And we subtract from the total the ASCII value of the largest common subsequence multiplied by 2, since the substring occurs in both of the original strings
Good problem, fun to get to play around with facets of LCS
Full code below:
Comments
Post a Comment