Talentd
FleetCode
Make and follow sheets.Sign in

Find Most Frequent Vowel and Consonant solution and explanation

EasyCountingHash TableString4 min read89.4% acceptanceUpdated 24 Mar 2026
Find Most Frequent Vowel and Consonant | Leetcode 3541 | Hindi Explaination | Leetcode | DSACode with Deepak · 10:22 · 1K viewsWatch on YouTube
9 more videos on this problem
  1. Find Most Frequent Vowel and Consonant | Leetcode 3541 | Hindi Explaination | Leetcode | DSACode with Deepak · 10:22 · 1K views
  2. Find Most Frequent Vowel and Consonant | | LeetCode 3541 | Java Code | Developer CoderDeveloper Coder · 6:35 · 600 views
  3. 3541. Find Most Frequent Vowel and Consonant | Leetcode Daily - PythonLeetcode Daily · 6:08 · 583 views
  4. LeetCode 3541 🔥 Find Most Frequent Vowel and Consonant | Daily Challenge | Java Solution + Dry Run Study Placement · 5:59 · 495 views
  5. Find Most Frequent Vowel and Consonant | LeetCode 3541 | Hash Table | Beginner FriendlyCode Harmony · 12:10 · 457 views
  6. Daily Leetcode #1992: Sep 13, 2025 - 3541. Find Most Frequent Vowel and ConsonantProgramming Live with Larry · 2:34 · 380 views
  7. 3541. Find Most Frequent Vowel and Consonant | String | Hash Table | LeetcodeRapid Syntax · 6:45 · 249 views
  8. Find Most Frequent Vowel and Consonant | LeetCode 3541 - Pythonimpoldev · 4:59 · 210 views
  9. Find Most Frequent Vowel and Consonant | LeetCode 3541(Easy) | Codeminttcodemintt · 4:06 · 137 views
  10. 3541. Find Most Frequent Vowel and Consonant , Leetcode question Solution , Betas 100%Dravid sharma · 2:20 · 126 views

The 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.

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.

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.

Solution

We first use a hash table or an array of length 26, cnt, to count the frequency of each letter. Then, we iterate through this table to find the most frequent vowel and consonant, and return the sum of their frequencies.

We can use a variable a to record the maximum frequency of vowels and another variable b to record the maximum frequency of consonants. During the iteration, if the current letter is a vowel, we update a; otherwise, we update b.

Finally, we return a + b.

The time complexity is O(n), where n is the length of the string. The space complexity is O(|\Sigma|), where |\Sigma| is the size of the alphabet, which is 26 in this case.

class Solution:
    def maxFreqSum(self, s: str) -> int:
        cnt = Counter(s)
        a = b = 0
        for c, v in cnt.items():
            if c in "aeiou":
                a = max(a, v)
            else:
                b = max(b, v)
        return a + b
class Solution {
    public int maxFreqSum(String s) {
        int[] cnt = new int[26];
        for (char c : s.toCharArray()) {
            ++cnt[c - 'a'];
        }
        int a = 0, b = 0;
        for (int i = 0; i < cnt.length; ++i) {
            char c = (char) (i + 'a');
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                a = Math.max(a, cnt[i]);
            } else {
                b = Math.max(b, cnt[i]);
            }
        }
        return a + b;
    }
}
class Solution {
public:
    int maxFreqSum(string s) {
        int cnt[26]{};
        for (char c : s) {
            ++cnt[c - 'a'];
        }
        int a = 0, b = 0;
        for (int i = 0; i < 26; ++i) {
            char c = 'a' + i;
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                a = max(a, cnt[i]);
            } else {
                b = max(b, cnt[i]);
            }
        }
        return a + b;
    }
};
function maxFreqSum(s: string): number {
    const cnt: number[] = Array(26).fill(0);
    for (const c of s) {
        ++cnt[c.charCodeAt(0) - 97];
    }
    let [a, b] = [0, 0];
    for (let i = 0; i < 26; ++i) {
        const c = String.fromCharCode(i + 97);
        if ('aeiou'.includes(c)) {
            a = Math.max(a, cnt[i]);
        } else {
            b = Math.max(b, cnt[i]);
        }
    }
    return a + b;
}
public class Solution {
    public int MaxFreqSum(string s) {
        int[] cnt = new int[26];
        foreach (char c in s) {
            cnt[c - 'a']++;
        }
        int a = 0, b = 0;
        for (int i = 0; i < 26; i++) {
            char c = (char)('a' + i);
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                a = Math.Max(a, cnt[i]);
            } else {
                b = Math.Max(b, cnt[i]);
            }
        }
        return a + b;
    }
}
func maxFreqSum(s string) int {
	cnt := [26]int{}
	for _, c := range s {
		cnt[c-'a']++
	}
	a, b := 0, 0
	for i := range cnt {
		c := byte(i + 'a')
		if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
			a = max(a, cnt[i])
		} else {
			b = max(b, cnt[i])
		}
	}
	return a + b
}
use std::collections::HashMap;

impl Solution {
    pub fn max_freq_sum(s: String) -> i32 {
        let mut cnt: HashMap<char, i32> = HashMap::new();
        for c in s.chars() {
            *cnt.entry(c).or_insert(0) += 1;
        }
        let mut a = 0;
        let mut b = 0;
        for (c, v) in cnt {
            if "aeiou".contains(c) {
                a = a.max(v);
            } else {
                b = b.max(v);
            }
        }
        a + b
    }
}
Try this approach in the editor

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

Frequently asked questions

Is Find Most Frequent Vowel and Consonant easy or hard?

Find Most Frequent Vowel and Consonant is considered an Easy problem. It focuses on basic string traversal and frequency counting using a hash table or array. The key skill tested is recognizing that a single-pass counting approach solves the problem efficiently.

Find Most Frequent Vowel and Consonant Python/Java solution

Python and Java solutions typically use a frequency dictionary or an array of size 26. Iterate through the string, increment counts, then compute the maximum frequency for vowels and consonants separately. The algorithm runs in O(n) time and constant space.

How to solve Find Most Frequent Vowel and Consonant in O(n)?

Iterate through the string once and maintain a frequency array or hash map. After counting occurrences, check each character and determine whether it is a vowel or consonant, updating two maximum counters. This avoids repeated scans and guarantees linear time complexity.

What is the best approach for Find Most Frequent Vowel and Consonant?

The best approach is frequency counting in a single pass. Use either a hash table or a fixed array of size 26 to store counts of each character, then track the maximum frequency separately for vowels and consonants. This runs in O(n) time and O(1) space because the alphabet size is constant.

Is Find Most Frequent Vowel and Consonant asked at Google/Amazon/Meta?

Problems involving character frequency counting appear frequently in interviews at companies like Amazon, Google, and Meta. Variations include finding the most frequent character, counting vowels, or grouping characters by frequency. The core idea relies on hash tables or counting arrays.

What data structure is used in Find Most Frequent Vowel and Consonant?

The common data structures are a hash table (dictionary) or a fixed-size integer array for character frequency counting. Both allow constant-time updates while scanning the string. The array approach is slightly faster because it avoids hashing overhead.

What is the time complexity of Find Most Frequent Vowel and Consonant?

The optimal solution runs in O(n) time, where n is the length of the string. Each character is processed once to update its frequency. A second pass through at most 26 letters determines the most frequent vowel and consonant.

Comments

Now write it yourselfClose this page and solve Find Most Frequent Vowel and Consonant in the editor, without looking.
Practise Find Most Frequent Vowel and Consonant