Remove Letter to Equalize Frequency

You are given a 0-indexed string word, consisting of lowercase English letters. Your task is to select one index and remove the letter at that index from word such that the frequency of every remaining letter is equal.

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

Note:

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

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.

Now Your Turn!

Pick the correct output for the given input

Input: word = "abcdef"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

0
class Solution {
public:
bool equalFrequency(string word) {
// Your code goes here
}
};
 
Test Case

Input:

Word