3085. 成为 K 特殊字符串需要删除的最少字符数
题目链接:3085. 成为 K 特殊字符串需要删除的最少字符数
代码如下:
class Solution {
public:
int minimumDeletions(string word, int k) {
unordered_map<char, int> cnt;
for (char ch : word) {
cnt[ch]++;
}
int res = word.size();
for (unordered_map<char, int>::iterator it1 = cnt.begin();it1 != cnt.end();it1++) {
int deleted = 0;
for (unordered_map<char, int>::iterator it2 = cnt.begin();it2 != cnt.end();it2++) {
if (it1->second > it2->second) {
deleted += it2->second;
}
else if (it2->second > it1->second + k) {
deleted += it2->second - (it1->second + k);
}
}
res = min(res, deleted);
}
return res;
}
};