Fewest Character Deletions So Every Letter Frequency Is Unique

Last updated: November 29, 2026

Quick Overview

A Gboard input buffer is a string of lowercase letters. Return the minimum number of characters to delete so that no two distinct letters that remain have the same frequency.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A Gboard input buffer is a string of lowercase letters. Return the minimum number of characters to delete so that no two distinct letters that remain have the same frequency.

Google screens use frequency puzzles like this to check counting plus a small greedy fix-up. The interviewer wants to see that you process frequencies from high to low and lower each one only as far as needed.

What the Interviewer Expects
  • Count letter frequencies
  • Sort frequencies descending
  • Cap each frequency just below the previous kept one
  • Allow frequencies to drop to zero (letter removed entirely)
  • O(n + k log k) time for k distinct letters
Key Topics to Cover
Greedy
Hash map counting
Sorting
Strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the resulting string, not only the deletion count.
  • You may also insert characters; what is the minimum number of edits?
  • Do it in O(n) without sorting.
  • Frequencies must be unique and form a contiguous range 1..m.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: lowercase string s. Output: minimum deletions such that all nonzero letter counts are pairwise distinct. A letter deleted entirely has count 0, and zeros do not conflict with each other....

High-Level Design

Get the counts and sort them descending. The largest count never needs to shrink. Each next count must be strictly below the previous kept count, so its allowed value is min(count, prev - 1), floore...


Submit Your Answer
Markdown supported