2423. Remove Letter To Equalize Frequency LeetCode Solution

In this guide, you will get 2423. Remove Letter To Equalize Frequency LeetCode Solution with the best time and space complexity. The solution to Remove Letter To Equalize Frequency problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove Letter To Equalize Frequency solution in C++
  4. Remove Letter To Equalize Frequency solution in Java
  5. Remove Letter To Equalize Frequency solution in Python
  6. Additional Resources
2423. Remove Letter To Equalize Frequency LeetCode Solution image

Problem Statement of Remove Letter To Equalize Frequency

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.

See also  890. Find and Replace Pattern LeetCode Solution

Constraints:

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

Complexity Analysis

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

2423. Remove Letter To Equalize Frequency LeetCode Solution in C++

class Solution {
 public:
  bool equalFrequency(string word) {
    vector<int> count(26);

    for (const char c : word)
      ++count[c - 'a'];

    // Try to remove each letter, then check if the frequency of all the letters
    // in `word` are equal.
    for (const char c : word) {
      --count[c - 'a'];
      if (equalFreq(count))
        return true;
      ++count[c - 'a'];
    }

    return false;
  }

 private:
  static constexpr int kMax = 101;

  bool equalFreq(const vector<int>& count) {
    int minfreq = kMax;
    int maxfreq = 0;
    for (const int freq : count)
      if (freq > 0) {
        minfreq = min(minfreq, freq);
        maxfreq = max(maxfreq, freq);
      }
    return minfreq == maxfreq;
  }
};
/* code provided by PROGIEZ */

2423. Remove Letter To Equalize Frequency LeetCode Solution in Java

class Solution {
  public boolean equalFrequency(String word) {
    int[] count = new int[26];

    for (final char c : word.toCharArray())
      ++count[c - 'a'];

    // Try to remove each letter, then check if the frequency of all the letters
    // in `word` are equal.
    for (final char c : word.toCharArray()) {
      --count[c - 'a'];
      if (equalFreq(count))
        return true;
      ++count[c - 'a'];
    }

    return false;
  }

  private static final int kMax = 101;

  private boolean equalFreq(int[] count) {
    int minfreq = kMax;
    int maxfreq = 0;
    for (final int freq : count)
      if (freq > 0) {
        minfreq = Math.min(minfreq, freq);
        maxfreq = Math.max(maxfreq, freq);
      }
    return minfreq == maxfreq;
  }
}
// code provided by PROGIEZ

2423. Remove Letter To Equalize Frequency LeetCode Solution in Python

class Solution:
  def equalFrequency(self, word: str) -> bool:
    count = collections.Counter(word)

    # Try to remove each letter, then check if the frequency of all the letters
    # in `word` are equal.
    for c in word:
      count[c] -= 1
      if count[c] == 0:
        del count[c]
      if min(count.values()) == max(count.values()):
        return True
      count[c] += 1

    return False
# code by PROGIEZ

Additional Resources

See also  2079. Watering Plants LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.