Skip to main content

Minimum Operations to Make Character Frequencies Equal - Solution & Explanation

HardHash TableStringDynamic ProgrammingCounting3 min readAsked at: Google, Tiktok
Practice this problem

Problem Statement

You are given a string s.

A string t is called good if all characters of t occur the same number of times.

You can perform the following operations any number of times:

  • Delete a character from s.
  • Insert a character in s.
  • Change a character in s to its next letter in the alphabet.

Note that you cannot change 'z' to 'a' using the third operation.

Return the minimum number of operations required to make s good.

 

Example 1:

Input: s = "acab"

Output: 1

Explanation:

We can make s good by deleting one occurrence of character 'a'.

Example 2:

Input: s = "wddw"

Output: 0

Explanation:

We do not need to perform any operations since s is initially good.

Example 3:

Input: s = "aaabc"

Output: 2

Explanation:

We can make s good by applying these operations:

  • Change one occurrence of 'a' to 'b'
  • Insert one occurrence of 'c' into s

 

Constraints:

  • 3 <= s.length <= 2 * 104
  • s contains only lowercase English letters.

Approach Overview

Problem Overview: You are given a string and can perform operations that insert, delete, or convert characters. The goal is to make every character that appears in the string have the same frequency with the minimum number of operations.

Approach 1: Frequency Enumeration with Greedy Balancing (O(26 * maxFreq) time, O(1) space)

Start with a counting pass to compute the frequency of each character (26 lowercase letters). The key observation: in the final string, every used character must appear exactly k times. Enumerate all possible target frequencies k from 1 to the maximum existing frequency. For each character count c, compute the cheapest way to reach k: delete extra characters if c > k, insert missing characters if c < k, or convert surplus characters from one letter into deficits of another. Greedily match surplus counts with deficits to minimize operations. The minimal cost across all k values is the answer.

Approach 2: Dynamic Programming on Alphabet Frequencies (O(26 * maxFreq) time, O(26) space)

After computing character counts using a hash table or fixed array, iterate over possible target frequencies k. For each letter, decide whether to delete all occurrences, keep exactly k, or convert extra characters to help satisfy deficits in other letters. A small dynamic programming state tracks the minimum cost while processing the alphabet left to right, carrying over surplus characters that can be converted later. This avoids double‑counting conversions and guarantees the minimum operations for each target frequency.

Approach 3: Brute Force Enumeration of Target Sets (O(2^26) theoretical, impractical)

A naive formulation tries every subset of characters that will remain in the final string and forces each chosen character to have the same frequency. For each subset, compute the cost to insert, delete, or convert characters accordingly. While this clarifies the structure of the problem, the search space is exponential and not feasible in practice.

Recommended for interviews: Enumerating the target frequency combined with counting and greedy balancing is the expected solution. It shows you recognize that the final state must use a single frequency k and reduces the problem to evaluating costs for each candidate. The dynamic programming variant demonstrates deeper control over conversions between characters, which interviewers often appreciate for hard string optimization problems.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Frequency Enumeration + GreedyO(26 * maxFreq)O(1)Best general solution for lowercase strings; simple and efficient
Dynamic Programming on FrequenciesO(26 * maxFreq)O(26)When carefully tracking conversions between surplus and deficit characters
Subset Brute ForceO(2^26)O(1)Conceptual baseline; not practical for real inputs

Video Solution

3389. Minimum Operations to Make Character Frequencies Equal (Leetcode Hard)Programming Live with Larry750 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Minimum Operations to Make Character Frequencies Equal easy or hard?
LeetCode classifies this problem as Hard with an acceptance rate around 26%. The difficulty comes from recognizing that the final configuration must use a single frequency and efficiently balancing surplus and deficit characters across the alphabet.
Minimum Operations to Make Character Frequencies Equal Python/Java solution
Most implementations start with a 26-length frequency array, iterate over possible target frequencies, and compute operation costs for each letter. The same logic works in Python, Java, C++, and Go because it relies only on counting and simple arithmetic operations.
How to solve Minimum Operations to Make Character Frequencies Equal in O(n)?
Start by counting character frequencies in O(n). Then enumerate possible target frequencies k and compute the minimum operations using surplus and deficit balancing across characters. Since the alphabet size is fixed (26), the additional work is constant relative to n, giving an effective linear-time solution.
What is the best approach for Minimum Operations to Make Character Frequencies Equal?
The most effective approach is to count character frequencies and enumerate a target frequency k. For each possible k, compute the minimum cost to adjust every character count using deletions, insertions, or conversions. This reduces the search space to at most maxFreq candidates and runs in O(26 * maxFreq) time with constant extra space.
Is Minimum Operations to Make Character Frequencies Equal asked at Google/Amazon/Meta?
Hard string optimization problems involving frequency balancing and dynamic programming are common at companies like Google, Amazon, and Meta. This problem tests counting, greedy reasoning, and DP over a small alphabet, patterns frequently seen in senior-level interview rounds.
What data structure is used in Minimum Operations to Make Character Frequencies Equal?
A frequency array or hash table is used to count occurrences of each character. The solution then applies enumeration and sometimes dynamic programming to compute the cheapest sequence of insert, delete, or convert operations.
What is the time complexity of Minimum Operations to Make Character Frequencies Equal?
The optimal solution runs in O(26 * maxFreq) time. You first count the 26 lowercase letter frequencies, then evaluate each possible target frequency up to the maximum count. Space complexity is O(1) because the alphabet size is fixed.

Ready to solve this problem?

Practice Minimum Operations to Make Character Frequencies Equal with our built-in code editor and test cases.

Practice on FleetCode