#1647

Minimum Deletions to Make Character Frequencies Unique

specialist · 680 · lc medium +30 · verified · 61.5% accepted · 5,052 likes · top 62%

Description

A string s is called good if no two distinct characters share the same frequency. Given s, return the minimum number of character deletions needed to make it good.

Example 1:

Input: s = "aab"
Output: 0
Explanation: s is already good.

Example 2:

Input: s = "aaabbbcc"
Output: 2
Explanation: You can delete two 'b's resulting in the good string "aaabcc".
Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".

Example 3:

Input: s = "ceabaacb"
Output: 2
Explanation: You can delete both 'c's resulting in the good string "eabaab".
Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).

Code

1
2
3