Minimum Deletions to Make Character Frequencies Unique - Problem
A string s is called good if there are no two different characters in s that have the same frequency.
Given a string s, return the minimum number of characters you need to delete to make s good.
The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.
Input & Output
Example 1 — Basic Case
$
Input:
s = "aab"
›
Output:
0
💡 Note:
Character frequencies: a=2, b=1. All frequencies are unique, so no deletions needed.
Example 2 — Need Deletions
$
Input:
s = "aaabbbcc"
›
Output:
2
💡 Note:
Character frequencies: a=3, b=3, c=2. We have duplicate frequency 3. Reduce one character's frequency: 3→1, requiring 2 deletions.
Example 3 — Complex Case
$
Input:
s = "ceabaacb"
›
Output:
2
💡 Note:
Character frequencies: a=3, b=2, c=2, e=1. Two characters have frequency 2. Reduce one to frequency 1, but that conflicts with e=1, so reduce to 0. Total: 2 deletions.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code