#1941
Easy Algorithms Check if all characters have equal number of occurrences
Hash Table String Counting
79.4% acceptance
Feb 25, 2026
1035
29
Given a string s, return true if s is a good string, or false otherwise.
A string s is good if all the characters that appear in s have the same number of occurrences (i.e., the same frequency).
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn are_occurrences_equal(s: String) -> bool {
let mut freq = [0; 26];
for b in s.bytes() {
freq[(b - b'a') as usize] += 1;
}
let target = freq.iter().filter(|&&x| x > 0).next().unwrap();
freq.iter().filter(|&&x| x > 0).all(|x| x == target)
}
}