Find Most Frequent Vowel and Consonant - Problem
You are working as a linguistic analyst for a language learning app. Given a string s consisting of lowercase English letters, your task is to identify the most popular vowel and consonant characters.
Your mission:
- Find the vowel (one of
'a','e','i','o', or'u') with the maximum frequency - Find the consonant (all other letters excluding vowels) with the maximum frequency
- Return the sum of these two maximum frequencies
Special cases:
- If multiple vowels or consonants tie for the highest frequency, you can choose any one of them
- If there are no vowels or no consonants in the string, consider their frequency as
0
Example: In the string "programming", the vowel 'o' appears 1 time and consonant 'r' appears 2 times, so we return 1 + 2 = 3.
Input & Output
example_1.py โ Basic case
$
Input:
s = "programming"
โบ
Output:
3
๐ก Note:
Vowels: 'o' appears 1 time, 'a' and 'i' appear 1 time each. Max vowel frequency = 1. Consonants: 'r' appears 2 times, 'm' appears 2 times, others appear 1 time each. Max consonant frequency = 2. Result: 1 + 2 = 3.
example_2.py โ Equal frequencies
$
Input:
s = "aabbcc"
โบ
Output:
4
๐ก Note:
Vowels: 'a' appears 2 times. Max vowel frequency = 2. Consonants: 'b' appears 2 times, 'c' appears 2 times. Max consonant frequency = 2. Result: 2 + 2 = 4.
example_3.py โ Only vowels
$
Input:
s = "aeiou"
โบ
Output:
1
๐ก Note:
Vowels: Each vowel 'a', 'e', 'i', 'o', 'u' appears 1 time. Max vowel frequency = 1. Consonants: None present. Max consonant frequency = 0. Result: 1 + 0 = 1.
Constraints
- 1 โค s.length โค 1000
- s consists of lowercase English letters only
- Vowels are defined as 'a', 'e', 'i', 'o', 'u'
- All other lowercase letters are consonants
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Trackers
Set up frequency counters and maximum trackers for both vowels and consonants
2
Process Each Character
For each character, increment its frequency and update the appropriate maximum
3
Categorize Efficiently
Use a vowel set for O(1) lookup to determine if character is vowel or consonant
4
Return Sum
Add the maximum vowel frequency and maximum consonant frequency
Key Takeaway
๐ฏ Key Insight: By tracking maximums during frequency counting, we eliminate the need for a separate search phase, achieving optimal O(n) performance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code