#3541
Easy Algorithms Find most frequent vowel and consonant
Hash Table String Counting
89.3% acceptance
Feb 25, 2026
423
15
You are given a string s consisting of lowercase English letters.
Find the vowel ('a','e','i','o','u') with maximum frequency and the consonant with maximum frequency.
Return the sum of the two frequencies. If no vowels or no consonants, frequency is 0.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_freq_sum(s: String) -> i32 {
let vowels = b"aeiou";
let mut freq = [0i32; 26];
for b in s.bytes() {
freq[(b - b'a') as usize] += 1;
}
let max_vowel = (0..26u8)
.filter(|&i| vowels.contains(&(b'a' + i)))
.map(|i| freq[i as usize])
.max()
.unwrap_or(0);
let max_consonant = (0..26u8)
.filter(|&i| !vowels.contains(&(b'a' + i)))
.map(|i| freq[i as usize])
.max()
.unwrap_or(0);
max_vowel + max_consonant
}
}