LeetCode solutions

4138. Minimum Deletion Cost to Make All Characters Equal

My accepted Python solution to LeetCode problem 4138, Minimum Deletion Cost to Make All Characters Equal, running in 59ms.

  • Difficulty: Medium
  • Python
  • Runtime 59ms
  • Memory 30.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 59ms, memory 30.5MB, accepted 2025-12-29.

python
class Solution:
    def minCost(self, s: str, cost: List[int]) -> int:
        # Total cost of deleting everything
        total_cost = sum(cost)
        
        # For each character, compute sum of costs of positions with that char
        # We want to keep the character with maximum total cost
        char_cost = {}
        for i, c in enumerate(s):
            char_cost[c] = char_cost.get(c, 0) + cost[i]
        
        # Minimum deletion cost = total - max cost we can keep
        max_keep = max(char_cost.values())
        return total_cost - max_keep

Source