Skip to main content
Back to problems
#2423
Easy Algorithms

Remove letter to equalize frequency

Hash Table String Counting
19.2% acceptance
Feb 25, 2026
793
1353
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.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn equal_frequency(word: String) -> bool {
    let n = word.len();
    let bytes = word.as_bytes();
    for i in 0..n {
      let mut freq = [0i32; 26];
      for j in 0..n {
        if j != i {
          freq[(bytes[j] - b'a') as usize] += 1;
        }
      }
      let nonzero: Vec<i32> = freq.iter().filter(|&&v| v > 0).cloned().collect();
      if nonzero.is_empty() {
        return true;
      }
      let first = nonzero[0];
      if nonzero.iter().all(|&v| v == first) {
        return true;
      }
    }
    false
  }
}