## Similar Problems

Similar Problems not available

# Remove Letter To Equalize Frequency - Leetcode Solution

## Companies:

LeetCode: Remove Letter To Equalize Frequency Leetcode Solution

Difficulty: Easy

Topics: string hash-table

Problem Statement:

Given a string s consisting of lowercase English letters, you can perform the following operation any number of times:

- Remove any substring of s
- Return the minimum possible number of letters you have to remove to make all the letters in the string appear the same number of times.

Example 1:

Input: "aba" Output: 1 Explanation: Remove the letter 'a' to get "bb" which is equalized in frequency.

Example 2:

Input: "aab" Output: 0 Explanation: S is already repeated letter string.

Solution:

The problem can be solved by first counting the frequency of all the letters in the given string S, then finding the most frequently occurring letter and the least frequently occurring letter present in the string S. After that, we check if removing the most or least frequently occurring letter one by one can balance the frequency. We return the minimum number of removals required to balance frequency.

Algorithm:

- Find the frequency of each letter in the input string.
- Get the maximum frequency of any letter f_max and minimum frequency of any letter f_min.
- If all the letters in the string occur with the same frequency, then the answer is 0.
- Otherwise, we can either remove the most frequent letter or the least frequent letter. We need to check both cases and return the minimum of the two removal counts.
- If we remove the most frequent letter, then we need to subtract the frequency of the most frequent letter from the total frequency count and add it to the frequency of the least frequent letter. We also update the maximum frequency of any letter and the minimum frequency of any letter that are present after removing the most frequent letter.
- If we remove the least frequent letter, then we need to subtract the frequency of the least frequent letter from the total frequency count and add it to the frequency of the most frequent letter. We also update the maximum frequency of any letter and the minimum frequency of any letter that are present after removing the least frequent letter.
- We repeat steps 4-6 until we have equal frequency of all letters.

Pseudo Code:

function minRemovals(s: string) -> int: freq = [0] * 26 n = len(s) for i in range(n): freq[ord(s[i]) - ord('a')] += 1

```
# get max and min frequency of any letter
f_max = max(freq)
f_min = min(x for x in freq if x > 0)
if f_max == f_min:
return 0
removals = float('inf')
for c in range(26):
if freq[c] > 0:
# get new max and min frequency
f_new_max = max(f_max - freq[c], f_min)
f_new_min = min(f_min + freq[c], f_max)
if f_new_max == f_new_min:
removals = min(removals, n - freq[c])
else:
removals = min(removals, n - f_new_max*f_max - f_new_min*f_min)
return removals
```

Time Complexity: O(26 * n) = O(n) Space Complexity: O(26) = O(1)

## Remove Letter To Equalize Frequency Solution Code

`1`