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
Real-Time Character AnalysisVowel Trackinga:1o:1i:1Max Vowel Frequency: 1Consonant Trackingr:2m:2g:1Max Consonant Frequency: 2Final Result1 + 2 = 3โœจ One pass through string: O(n) time complexityString: "programming" โ†’ Analyzed in single efficient pass
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.
Asked in
Google 15 Amazon 23 Meta 8 Microsoft 12
18.2K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen