Minimum Deletions to Make Character Frequencies Unique

Medium Topics Company Tags

Delete as few characters as possible from s so that every remaining letter has a different frequency. Only letters still present matter: several letters may be deleted entirely. Return the minimum number of deletions.

Example 1:

Input: s = "aabb"

Output: 1

Explanation: Both letters initially occur twice. Delete one occurrence of either letter to obtain positive frequencies 2 and 1.

Example 2:

Input: s = "aaabbc"

Output: 0

Explanation: The positive frequencies are already 3 for a, 2 for b, and 1 for c. They are all distinct.

Example 3:

Input: s = "abcd"

Output: 3

Explanation: Each of four letters occurs once. Keep one letter and delete the other three so that only one positive frequency remains.

Constraints:

  • 1 <= s.length <= 100000
  • Only lowercase English letters occur.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.