#3120
Easy Algorithms Count the number of special characters i
Hash Table String
66.7% acceptance
Feb 23, 2026
171
5
You are given a string word. A letter is called special if it appears both in lowercase and uppercase in word.
Return the number of special letters in word.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn number_of_special_chars(word: String) -> i32 {
let mut lower = [false; 26];
let mut upper = [false; 26];
for b in word.bytes() {
if b.is_ascii_lowercase() {
lower[(b - b'a') as usize] = true;
} else {
upper[(b - b'A') as usize] = true;
}
}
(0..26).filter(|&i| lower[i] && upper[i]).count() as i32
}
}