Delete String
Given two strings and a cost array, find the minimum cost to make the strings equal by deleting characters. Deletions may be made from either string, in any order.
Each lowercase letter has its own deletion cost: costs[0] is the price of removing one 'a', costs[1] one 'b', and so on through costs[25] for 'z'. Removing a letter costs the same whichever string it came from, and the total is the sum over every deletion in both strings.
costs = [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] s1 = abb s2 = bba
2
With a costing 1 and b costing 2, delete the a from each string, paying 1 each time. Both strings become "bb", for a total of 2. Every other outcome is worse: leaving a single b costs 6, leaving a single a costs 8, and deleting everything costs 10.
costs = [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] s1 = abc s2 = cba
6
The strings are reverses of each other, so no two characters survive in the same order in both and whatever is left must be a single letter. Keeping the letter that is most expensive to delete, c, means deleting a and b from each string: (1 + 2) + (2 + 1) = 6. Keeping b would cost 8 and keeping a would cost 10.
1 <= s1.length, s2.length <= 1000s1ands2consist of lowercase English letterscosts[i]is the deletion cost of the letter'a' + i, with0 <= costs[i] <= 10^4costsis indexed by letter, so only its first 26 entries are ever read- The answer fits in a 32-bit signed integer