Watch 10 video solutions for Minimum Deletion Cost to Make All Characters Equal, a medium level problem involving Array, Hash Table, String. This walkthrough by Rapid Syntax has 244 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s of length n and an integer array cost of the same length, where cost[i] is the cost to delete the ith character of s.
You may delete any number of characters from s (possibly none), such that the resulting string is non-empty and consists of equal characters.
Return an integer denoting the minimum total deletion cost required.
Example 1:
Input: s = "aabaac", cost = [1,2,3,4,1,10]
Output: 11
Explanation:
Deleting the characters at indices 0, 1, 2, 3, 4 results in the string "c", which consists of equal characters, and the total cost is cost[0] + cost[1] + cost[2] + cost[3] + cost[4] = 1 + 2 + 3 + 4 + 1 = 11.
Example 2:
Input: s = "abc", cost = [10,5,8]
Output: 13
Explanation:
Deleting the characters at indices 1 and 2 results in the string "a", which consists of equal characters, and the total cost is cost[1] + cost[2] = 5 + 8 = 13.
Example 3:
Input: s = "zzzzz", cost = [67,67,67,67,67]
Output: 0
Explanation:
All characters in s are equal, so the deletion cost is 0.
Constraints:
n == s.length == cost.length1 <= n <= 1051 <= cost[i] <= 109s consists of lowercase English letters.Problem Overview: You are given a string and a deletion cost for each character. Delete characters so that every remaining character in the string is the same. The goal is to minimize the total deletion cost.
Approach 1: Enumerate Target Character with Cost Grouping (O(n) time, O(k) space)
The key observation: the final string must contain only one distinct character. Instead of thinking about which characters to delete, think about which character you want to keep. For each character c, keep all occurrences of c and delete every other character. Use a hash map to accumulate the total cost associated with each character while iterating through the string.
Compute the total deletion cost of the entire string. Then track how much cost is associated with each character type. If you keep character c, you avoid deleting those positions, meaning the deletion cost becomes totalCost - costOf[c]. Iterate through all unique characters and choose the minimum possible deletion cost.
This works because keeping more occurrences of a character always reduces the deletion cost. The optimal strategy is simply to keep the character whose total cost contribution is the largest. A hash table makes grouping efficient, while iterating through the string once collects all required information.
Recommended for interviews: The grouping + enumeration approach is what interviewers expect. It demonstrates that you reframed the problem from “which characters to delete” into “which character to keep”. The implementation uses a simple array traversal and hash map aggregation, achieving linear time and minimal extra space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Character Enumeration | O(n * k) | O(1) | When alphabet size is extremely small and clarity matters more than efficiency |
| Grouping + Enumeration with Hash Map | O(n) | O(k) | General case. Efficient for large strings and multiple character types |