Delete characters at minimum total cost so no two adjacent letters are equal

Quick Overview

Given a string and the cost of deleting each character, find the cheapest set of deletions that leaves no two identical letters next to each other. Tests greedy reasoning on strings, a proof that a local choice is optimal, and a linear-time scan.

Delete characters at minimum total cost so no two adjacent letters are equal

Company: HSBC

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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`. ```text 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. ```text 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`.

Overview: Given a string and the cost of deleting each character, find the cheapest set of deletions that leaves no two identical letters next to each other. Tests greedy reasoning on strings, a proof that a local choice is optimal, and a linear-time scan.

HSBC logo
HSBC
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.