from collections import Counter
class Solution:
def minDeletions(self, s: str) -> int:
freq = Counter(s)
used = set()
deletions = 0
for char, f in freq.items():
while f > 0 and f in used:
f -= 1
deletions += 1
used.add(f)
return deletions
#include <unordered_map>
#include <unordered_set>
#include <string>
using namespace std;
class Solution {
public:
int minDeletions(string s) {
unordered_map<char, int> freq;
for (char c : s) freq[c]++;
unordered_set<int> used;
int deletions = 0;
for (auto it : freq) {
int f = it.second;
while (f > 0 && used.count(f)) {
f--;
deletions++;
}
used.insert(f);
}
return deletions;
}
};
import java.util.*;
class Solution {
public int minDeletions(String s) {
int[] freq = new int[26];
for (char c : s.toCharArray()) freq[c - 'a']++;
Set<Integer> used = new HashSet<>();
int deletions = 0;
for (int f : freq) {
while (f > 0 && used.contains(f)) {
f--;
deletions++;
}
if (f > 0) used.add(f);
}
return deletions;
}
}
var minDeletions = function(s) {
let freq = {};
for (let c of s) freq[c] = (freq[c]||0) + 1;
let used = new Set();
let deletions = 0;
for (let char in freq) {
let f = freq[char];
while (f > 0 && used.has(f)) {
f--;
deletions++;
}
used.add(f);
}
return deletions;
};
Given a string s
, you need to ensure that the frequency of each character in s
is unique. In other words, no two different characters can have the same frequency after making deletions. You are allowed to delete any number of characters from s
(possibly zero) to achieve this. The task is to return the minimum number of deletions required to make all character frequencies unique.
Constraints:
s = "aaabbbcc"
, you should return 2
because you can delete two 'b' or two 'a' or one 'a' and one 'b' to make the frequencies [3,2,2] into [3,2,1].
The core challenge is to ensure that the count (frequency) of each character in the string is unique. If two or more characters have the same frequency, we must delete some characters to make the frequencies distinct.
A naive approach would be to try all possible combinations of deletions, but this is highly inefficient. Instead, we can focus on the frequencies themselves: if two frequencies are the same, we can reduce one of them (by deleting characters) until it becomes unique or zero (removing that character entirely).
The main insight is that we can process the frequencies, and for each one, if it is already used, we decrement it until we find an unused frequency or reach zero. This ensures minimal deletions and guarantees uniqueness.
We use a set for used frequencies because lookups and insertions are O(1), making the algorithm efficient.
Let's consider s = "aaabbbcc"
.
Final frequencies are [3,2,1], all unique. The minimum deletions needed is 2.
Thus, the solution is linear in the length of the string, with constant extra space for the English alphabet.
The key to solving this problem efficiently is to focus on character frequencies and ensure uniqueness by decrementing duplicates. By using a set to track used frequencies and only deleting as necessary, we achieve a minimal number of deletions. The approach leverages hash maps and sets for fast lookups, resulting in a simple yet elegant solution that is both time and space efficient.