[LeetCode] Minimum Deletion Cost to Make All Characters Equal

3784. Minimum Deletion Cost to Make All Characters Equal

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 i^th 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
long long minCost(string s, vector<int>& cost) {
vector<long long> sum(26);
long long total = 0;

for(int i = 0; i < s.size(); i++) {
total += cost[i];
sum[s[i] - 'a'] += cost[i];
}

return total - *max_element(sum.begin(), sum.end());
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/minimum-deletion-cost-to-make-all-characters-equal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.