Remove Letter To Equalize Frequency - Problem

You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.

Return true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise.

Note:

  • The frequency of a letter x is the number of times it occurs in the string.
  • You must remove exactly one letter and cannot choose to do nothing.

Input & Output

Example 1 — Two Close Frequencies
$ Input: word = "abcc"
Output: true
💡 Note: Remove one 'c' to get "abc": a:1, b:1, c:1 - all frequencies equal
Example 2 — Cannot Equalize
$ Input: word = "aab"
Output: true
💡 Note: Remove one 'a' to get "ab": a:1, b:1 - all frequencies equal
Example 3 — Single Character Type
$ Input: word = "aaa"
Output: true
💡 Note: Remove one 'a' to get "aa": only character 'a' with frequency 2

Constraints

  • 1 ≤ word.length ≤ 105
  • word consists of lowercase English letters
  • You must remove exactly one character

Visualization

Tap to expand
Remove Letter To Equalize Frequency INPUT String: word = "abcc" a idx 0 b idx 1 c idx 2 c idx 3 Character Frequencies: a: 1 b: 1 c: 2 Goal: Remove exactly ONE letter to equalize all frequencies ALGORITHM STEPS 1 Count Frequencies Build freq map: {a:1, b:1, c:2} 2 Try Each Removal Simulate removing each char 3 Check Equality Verify all freqs are equal 4 Return Result True if any removal works Simulation: Remove 'a': b:1, c:2 - NO Remove 'b': a:1, c:2 - NO Remove 'c': a:1, b:1, c:1 - OK All equal at freq=1 Found valid! Return true FINAL RESULT After removing one 'c': a b c Result string: "abc" All Frequencies Equal! a:1 b:1 c:1 OUTPUT true Possible to equalize by removing one letter Key Insight: The optimal approach simulates removing each character once and checks if remaining frequencies are equal. Edge cases: Single char strings, all same chars (remove any), or when one char has freq = others + 1. Time: O(26 * n) = O(n) for checking each unique character removal. Space: O(26) = O(1) for frequency map. TutorialsPoint - Remove Letter To Equalize Frequency | Optimal Solution
Asked in
Google 15 Amazon 12 Facebook 8
32.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen