Skip to main content

Remove Letter To Equalize Frequency - Solution & Explanation

EasyHash TableStringCounting12 min readAsked at: Google, TCS, Bloomberg +1
Practice this problem

Problem Statement

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.

 

Example 1:

Input: word = "abcc"
Output: true
Explanation: Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.

Example 2:

Input: word = "aazz"
Output: false
Explanation: We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.

 

Constraints:

  • 2 <= word.length <= 100
  • word consists of lowercase English letters only.

Approach Overview

Problem Overview: You are given a string word. The task is to determine whether removing exactly one character can make the frequency of every remaining letter equal. After the removal, all characters that still appear must have identical counts.

Approach 1: Frequency Map and Validation (O(n) time, O(1) space)

Count the frequency of every character using a hash map or fixed array of size 26. Then simulate removing one occurrence of each distinct character. For each simulation, decrease its count by one and check whether the remaining non‑zero frequencies are identical. Validation is simple: iterate through the frequency map and ensure every non‑zero value matches the first seen value. Because the alphabet size is constant, this validation step is bounded by 26 operations. The main cost is building the frequency map from the string, which takes O(n) time with O(1) extra space. This approach works well because it avoids rebuilding the map for every index and only tests each character type once. It relies on efficient counting with a hash table or array.

Approach 2: Direct Simulation (O(n * 26) time, O(1) space)

This method tries removing each character position in the string and checks whether the remaining string has equal frequencies. For every index i, temporarily skip that character and recompute character counts for the rest of the string. After building the counts, verify that all non‑zero frequencies are equal. Because counting requires scanning up to n characters each time and there are n removal attempts, the straightforward version is O(n^2). However, with a fixed 26‑character alphabet you can optimize counting using a reusable array and simple decrements, making the effective complexity closer to O(n * 26). This approach is conceptually simpler and useful when first reasoning about the problem, especially if you think in terms of explicit simulation of the removal step. It mainly uses operations on arrays and concepts from string processing and counting.

Recommended for interviews: The frequency map validation approach is typically what interviewers expect. It shows you recognize that only character frequencies matter, not individual positions. Starting with direct simulation demonstrates understanding of the problem mechanics, but the optimized counting approach shows stronger algorithmic thinking and awareness of constant‑size alphabets.

Approach 1: Approach 1: Frequency Map and Validation

This approach involves constructing a frequency map of the letters in the word. By analyzing the frequencies, we can evaluate if removing one instance of a letter can equate the frequency of remaining letters. Check if the remaining frequencies can form a uniform distribution after removing one letter occurrence from the string.

The function uses a frequency counter to assess character frequency, then attempts to either bring one count down to match others by removal, or assess if removing a unique single occurrence leads to uniform frequencies.

Code

Python

Java

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), considering a fixed constant space of unique letters in the English alphabet.

Try this approach in the editor →

Approach 2: Approach 2: Direct Simulation

This approach involves simulating the removal of each letter to check if the remaining letters can have uniform frequencies. For each letter, temporarily remove it, recompute frequencies, and see if the remaining ones are equal.

The solution loops through each character, temporarily reduces its frequency, calculates the remaining frequencies, and checks for uniformity. If removing any letter results in a uniform distribution, it returns true; otherwise, false.

Code

C

JavaScript

Complexity

Time Complexity: O(n^2), since for each character, all frequencies are re-evaluated.
Space Complexity: O(1), given the constant letter count.

Try this approach in the editor →

Approach 3: Counting + Enumeration

First, we use a hash table or an array of length 26 named cnt to count the number of occurrences of each letter in the string.

Next, we enumerate the 26 letters. If letter c appears in the string, we decrement its count by one, then check whether the counts of the remaining letters are the same. If they are, return true. Otherwise, increment the count of c by one and continue to enumerate the next letter.

If the enumeration ends, it means that it is impossible to make the counts of the remaining letters the same by deleting one letter, so return false.

The time complexity is O(n + C^2), and the space complexity is O(C). Here, n is the length of the string word, and C is the size of the character set. In this problem, C = 26.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Frequency Map and Validation

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), considering a fixed constant space of unique letters in the English alphabet.

Approach 2: Direct Simulation

Time Complexity: O(n^2), since for each character, all frequencies are re-evaluated.
Space Complexity: O(1), given the constant letter count.

Counting + Enumeration

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Frequency Map and ValidationO(n)O(1)Best general solution when alphabet size is fixed (26 letters)
Direct SimulationO(n * 26)O(1)Good for understanding the problem or quick brute-force verification

Video Solution

Remove Letter To Equalize Frequency || LeetCode Biweekly Contest 88 || LeetCode EasyBinaryMagic2,982 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Remove Letter To Equalize Frequency easy or hard?
The problem is classified as Easy on LeetCode but can be tricky because you must reason about frequency patterns after a single removal. Once you focus on character counts instead of positions, the solution becomes straightforward with a simple validation check.
Remove Letter To Equalize Frequency Python/Java solution
Both Python and Java solutions typically use a frequency array or hash map to count characters. After building the counts, simulate removing one occurrence of each letter and verify whether the remaining frequencies are equal. This keeps the implementation simple while maintaining O(n) time complexity.
How to solve Remove Letter To Equalize Frequency in O(n)?
First count the frequency of each letter using an array or hash map. Then try decreasing the count of each character once and check if all remaining non-zero frequencies are equal. Because there are at most 26 characters, each validation step is constant time, giving an overall O(n) algorithm.
What is the best approach for Remove Letter To Equalize Frequency?
The most efficient approach uses a frequency map of the 26 lowercase letters and simulates removing one occurrence of each character type. After each simulated removal, check whether all remaining non-zero frequencies match. This runs in O(n) time with O(1) space because the alphabet size is constant.
Is Remove Letter To Equalize Frequency asked at Google/Amazon/Meta?
Frequency manipulation and counting problems like this appear frequently in interviews at companies such as Amazon, Google, and Meta. While this exact problem may vary, the underlying pattern—analyzing character counts with a hash table—is common in string interview questions.
What data structure is used in Remove Letter To Equalize Frequency?
The primary data structure is a hash table or a fixed-size integer array that stores character frequencies. Since the input consists of lowercase English letters, a 26-element array is usually the most efficient representation.
What is the time complexity of Remove Letter To Equalize Frequency?
The optimal solution runs in O(n) time where n is the length of the string. Building the frequency map requires one pass through the string, and validation checks at most 26 characters. Space complexity is O(1) since only a fixed-size frequency array is used.

Ready to solve this problem?

Practice Remove Letter To Equalize Frequency with our built-in code editor and test cases.

Practice on FleetCode