##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2423.html

# 2423. Remove Letter To Equalize Frequency

• Difficulty: Easy.
• Related Topics: .
• Similar Questions: Maximum Equal Frequency, Minimum Deletions to Make Character Frequencies Unique.

## 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 chose 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.

## Solution (Java, C++, Python)

• class Solution {
public boolean equalFrequency(String word) {
for (int i = 0; i < word.length(); ++i) {
int[] cnt = new int;
for (int j = 0; j < word.length(); ++j) {
if (j != i) {
++cnt[word.charAt(j) - 'a'];
}
}
Set<Integer> vis = new HashSet<>();
for (int v : cnt) {
if (v > 0) {
}
}
if (vis.size() == 1) {
return true;
}
}
return false;
}
}

• class Solution {
public:
bool equalFrequency(string word) {
for (int i = 0; i < word.size(); ++i) {
int cnt = {0};
for (int j = 0; j < word.size(); ++j) {
if (j != i) {
++cnt[word[j] - 'a'];
}
}
unordered_set<int> vis;
for (int v : cnt) {
if (v) {
vis.insert(v);
}
}
if (vis.size() == 1) {
return true;
}
}
return false;
}
};

• class Solution:
def equalFrequency(self, word: str) -> bool:
for i in range(len(word)):
cnt = Counter(word[:i] + word[i + 1 :])
if len(set(cnt.values())) == 1:
return True
return False


• func equalFrequency(word string) bool {
for i := range word {
cnt := make([]int, 26)
for j, c := range word {
if j != i {
cnt[c-'a']++
}
}
vis := map[int]bool{}
for _, v := range cnt {
if v > 0 {
vis[v] = true
}
}
if len(vis) == 1 {
return true
}
}
return false
}

• function equalFrequency(word: string): boolean {
const map = new Map();
for (const c of word) {
map.set(c, (map.get(c) ?? 0) + 1);
}
const count = new Map();
for (const v of map.values()) {
count.set(v, (count.get(v) ?? 0) + 1);
}
if (count.size === 1) {
return map.size == 1 || [...count.keys()] === 1;
}
if (count.size === 2) {
return [...count.entries()].some(
(v, i, arr) =>
(v === 1 || v - arr[i ^ 1] === 1) && v === 1,
);
}
return false;
}



Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).