Minimum Deletions to Make Character Frequencies Unique

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A string is *good* if no two distinct letters that appear in it occur the same number of times. Letters that have been deleted entirely (frequency 0) do not count. Given a string s, return the minimum number of characters you must delete from s to make it good.

Examples

Input: s = "aaabbbcc"
Output: 2
Frequencies are 3, 3, 2. Deleting two b's gives 3, 1, 2, which are all different.
Input: s = "abcd"
Output: 3
All four letters occur once; three of them must be deleted completely.

Constraints

  • 1 <= s.length <= 10^5
  • `s` contains only lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems