A string is good if no two different characters in it have the same frequency, where the frequency of a character is the number of times it appears in the string. For example, in "aab" the frequency of 'a' is 2 and the frequency of 'b' is 1.
Given a string s, return the minimum number of characters you need to delete to make s good.
Function Signature
def min_deletions(s: str) -> int:
pass
Rules
-
Deletions can be made at any positions. Only the number of remaining copies of each letter matters.
-
A letter whose copies are all deleted no longer appears in the string and has no frequency, so any number of letters can be removed completely without creating a conflict.
Constraints
-
1 <= len(s) <= 10^5
-
s
contains only lowercase English letters.
Examples
Input: s = "aab"
Output: 0
The frequencies are a:2 and b:1. They are already distinct, so no deletions are needed.
Input: s = "aaabbbcc"
Output: 2
The frequencies are a:3, b:3 and c:2. One of the two 3s has to come down, and 2 is already taken by c, so b drops to 1 at a cost of 2 deletions. One deletion cannot work: lowering a 3 to 2 collides with c, and lowering c to 1 leaves the two 3s.
Input: s = "ceabaacb"
Output: 2
The frequencies are a:3, b:2, c:2 and e:1. One of b and c must drop below 2, and 1 is held by e, so either that letter drops to 1 and e is deleted, or that letter is deleted entirely. Both options cost 2 deletions.