Skip to main content

Find Most Frequent Vowel and Consonant - Video Solutions

EasyHash TableStringCounting

Find Most Frequent Vowel and Consonant | Leetcode 3541 | Hindi Explaination | Leetcode | DSA

Code with Deepak
10:221,236 views
10 video solutions available

Find Most Frequent Vowel and Consonant - Video Solution

Watch 10 video solutions for Find Most Frequent Vowel and Consonant, a easy level problem involving Hash Table, String, Counting. This walkthrough by Code with Deepak has 1,236 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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.

 

Example 1:

Input: s = "successes"

Output: 6

Explanation:

  • The vowels are: 'u' (frequency 1), 'e' (frequency 2). The maximum frequency is 2.
  • The consonants are: 's' (frequency 4), 'c' (frequency 2). The maximum frequency is 4.
  • The output is 2 + 4 = 6.

Example 2:

Input: s = "aeiaeia"

Output: 3

Explanation:

  • The vowels are: 'a' (frequency 3), 'e' ( frequency 2), 'i' (frequency 2). The maximum frequency is 3.
  • There are no consonants in s. Hence, maximum consonant frequency = 0.
  • The output is 3 + 0 = 3.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters only.
Read full problem with examples

Approach Overview

Problem Overview: You are given a string and need to determine which vowel and which consonant appear most frequently. The task requires counting character occurrences, separating vowels (a, e, i, o, u) from consonants, and returning the highest frequency for each group.

Approach 1: Recounting for Each Character (Brute Force) (Time: O(26 * n), Space: O(1))

The simplest idea is to check every unique letter and count how many times it appears in the string. For each character, iterate through the entire string and update the maximum count depending on whether the character is a vowel or consonant. Since the alphabet size is fixed (26 letters), the extra factor remains constant, giving an effective complexity of O(26 * n). Space stays O(1) because no additional data structure is required. This method works but repeats the same counting work multiple times.

Approach 2: Hash Table Counting (Time: O(n), Space: O(1))

A more efficient method counts frequencies in a single pass using a hash table. Iterate through the string once and store character counts using a map like freq[c]++. After building the frequency map, iterate through the keys and check whether each character belongs to the vowel set (a, e, i, o, u). Track two running maximums: one for vowels and one for consonants. This reduces redundant work and guarantees O(n) time with constant extra space because the alphabet size is bounded.

Approach 3: Fixed-Size Frequency Array (Optimal Counting) (Time: O(n), Space: O(1))

Since characters are limited to lowercase English letters, a fixed array of size 26 is even simpler than a map. While scanning the string, increment count[c - 'a']. After the scan, iterate through the array and check whether each index corresponds to a vowel. Update the maximum vowel and consonant counts accordingly. This approach uses pure counting, avoids hash overhead, and keeps both time O(n) and space O(1).

Recommended for interviews: The frequency counting approach with either a hash map or a 26-length array is what interviewers expect. Brute force demonstrates understanding but repeats work unnecessarily. A single-pass counting strategy shows that you recognize the bounded alphabet and can reduce the problem to simple frequency tracking.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Recounting for Each Character (Brute Force)O(26 * n)O(1)Useful for explaining the basic idea before optimizing
Hash Table Frequency CountO(n)O(1)General solution when counting character frequencies
Fixed 26-Length Array CountingO(n)O(1)Best choice for lowercase alphabet strings with minimal overhead