Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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:
x 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.
Constraints:
2 <= word.length <= 100word consists of lowercase English letters only.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"abcc"
"aazz"
maximum-equal-frequency)minimum-deletions-to-make-character-frequencies-unique)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
class Solution {
public boolean equalFrequency(String word) {
int[] cnt = new int[26];
for (int i = 0; i < word.length(); ++i) {
++cnt[word.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
if (cnt[i] > 0) {
--cnt[i];
int x = 0;
boolean ok = true;
for (int v : cnt) {
if (v == 0) {
continue;
}
if (x > 0 && v != x) {
ok = false;
break;
}
x = v;
}
if (ok) {
return true;
}
++cnt[i];
}
}
return false;
}
}
// Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
func equalFrequency(word string) bool {
cnt := [26]int{}
for _, c := range word {
cnt[c-'a']++
}
for i := range cnt {
if cnt[i] > 0 {
cnt[i]--
x := 0
ok := true
for _, v := range cnt {
if v == 0 {
continue
}
if x > 0 && v != x {
ok = false
break
}
x = v
}
if ok {
return true
}
cnt[i]++
}
}
return false
}
# Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
class Solution:
def equalFrequency(self, word: str) -> bool:
cnt = Counter(word)
for c in cnt.keys():
cnt[c] -= 1
if len(set(v for v in cnt.values() if v)) == 1:
return True
cnt[c] += 1
return False
// Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
// class Solution {
// public boolean equalFrequency(String word) {
// int[] cnt = new int[26];
// for (int i = 0; i < word.length(); ++i) {
// ++cnt[word.charAt(i) - 'a'];
// }
// for (int i = 0; i < 26; ++i) {
// if (cnt[i] > 0) {
// --cnt[i];
// int x = 0;
// boolean ok = true;
// for (int v : cnt) {
// if (v == 0) {
// continue;
// }
// if (x > 0 && v != x) {
// ok = false;
// break;
// }
// x = v;
// }
// if (ok) {
// return true;
// }
// ++cnt[i];
// }
// }
// return false;
// }
// }
// Accepted solution for LeetCode #2423: Remove Letter To Equalize Frequency
function equalFrequency(word: string): boolean {
const cnt: number[] = new Array(26).fill(0);
for (const c of word) {
cnt[c.charCodeAt(0) - 97]++;
}
for (let i = 0; i < 26; ++i) {
if (cnt[i]) {
cnt[i]--;
let x = 0;
let ok = true;
for (const v of cnt) {
if (v === 0) {
continue;
}
if (x && v !== x) {
ok = false;
break;
}
x = v;
}
if (ok) {
return true;
}
cnt[i]++;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.