Given a string s and an array of integers cost where cost[i] is the cost of deleting the i-th character in s, return the minimum cost of deletions such that there are no two identical letters next to each other.
Note that you will delete the chosen characters at the same time, so deleting one character does not change the cost of deleting another character.
Function Signature
def min_deletion_cost(s: str, cost: list[int]) -> int:
Rules
-
The condition applies to the string left after all deletions: in that string, no two consecutive characters may be equal.
-
The characters you keep stay in their original order.
-
Return
0
when
s
already has no two equal adjacent letters.
Constraints
-
len(s) == len(cost)
-
1 <= len(s) <= 10^5
-
1 <= cost[i] <= 10^4
-
s
contains only lowercase English letters.
Examples
Input: s = "abaac", cost = [1,2,3,4,5]
Output: 3
Delete the 'a' at index 2 (cost 3). The result is "abac", which has no adjacent duplicates. Keeping index 2 and deleting index 3 instead would cost 4.
Input: s = "aabaa", cost = [1,2,3,4,1]
Output: 2
Delete the 'a' at index 0 (cost 1) and the 'a' at index 4 (cost 1), leaving "aba". Each pair of equal neighbors needs one deletion, and these are the cheapest choices for both pairs.
Input: s = "aaaa", cost = [4,9,3,8]
Output: 15
Only one 'a' can remain. Keeping the one at index 1 (cost 9) and deleting the rest costs 4 + 3 + 8 = 15.