#3581
Easy Algorithms Count odd letters from number
Hash Table String Simulation Counting
85.3% acceptance
Mar 31, 2026
8
2
You are given an integer n perform the following steps:
Convert each digit of n into its lowercase English word (e.g., 4 → "four", 1 → "one").
Concatenate those words in the original digit order to form a string s.
Return the number of distinct characters in s that appear an odd number of times.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn count_odd_letters(n: i32) -> i32 {
let words = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
];
let mut freq = [0u32; 26];
let mut num = n;
while num > 0 {
let digit = (num % 10) as usize;
for ch in words[digit].bytes() {
freq[(ch - b'a') as usize] += 1;
}
num /= 10;
}
// Handle n == 0 edge case (but constraint says n >= 1)
if n == 0 {
for ch in words[0].bytes() {
freq[(ch - b'a') as usize] += 1;
}
}
freq.iter().filter(|&&c| c > 0 && c % 2 == 1).count() as i32
}
}