Find Most Frequent Vowel and Consonant - Problem

You are given a string s consisting of lowercase English letters ('a' to 'z'). Your task is to:

  • 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 the two frequencies.

Note: If multiple vowels or consonants have the same maximum frequency, you may choose any one of them. If there are no vowels or no consonants in the string, consider their frequency as 0.

The frequency of a letter x is the number of times it occurs in the string.

Input & Output

Example 1 — Mixed Vowels and Consonants
$ Input: s = "hello"
Output: 3
💡 Note: Vowel frequencies: e=1, o=1. Consonant frequencies: h=1, l=2. Max vowel freq = 1, max consonant freq = 2. Result: 1 + 2 = 3
Example 2 — Repeated Vowel
$ Input: s = "aabbcc"
Output: 4
💡 Note: Vowel frequencies: a=2. Consonant frequencies: b=2, c=2. Max vowel freq = 2, max consonant freq = 2. Result: 2 + 2 = 4
Example 3 — Only Vowels
$ Input: s = "aeiou"
Output: 1
💡 Note: All characters are vowels with frequency 1 each. No consonants. Max vowel freq = 1, max consonant freq = 0. Result: 1 + 0 = 1

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of lowercase English letters

Visualization

Tap to expand
Find Most Frequent Vowel and Consonant INPUT String s = "hello" h C e V l C l C o V V = Vowel C = Consonant Frequency Count: Vowels: e=1, o=1 Consonants: h=1, l=2 s = "hello" ALGORITHM STEPS 1 Initialize Counters maxVowelFreq = 0 maxConsFreq = 0 2 Count Each Character Use freq array[26] for all letters 3 Track Max Frequencies If vowel: update maxVowel Else: update maxCons 4 Return Sum maxVowelFreq + maxConsFreq Single Pass Processing for char in s: freq[char]++ if isVowel: check max else: check max FINAL RESULT Frequency Analysis Max Vowel Frequency 'e' or 'o' appears 1 time = 1 Max Consonant Frequency 'l' appears 2 times = 2 + OUTPUT 3 1 + 2 = 3 OK - Sum of max frequencies Key Insight: Single Pass Optimization: Track maximum vowel and consonant frequencies simultaneously while iterating through the string once. Time Complexity: O(n), Space Complexity: O(1) using a fixed-size array of 26 for character frequencies. TutorialsPoint - Find Most Frequent Vowel and Consonant | Single Pass Optimized Approach
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
23.4K Views
Medium Frequency
~15 min Avg. Time
856 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