You are given a string s consisting of lowercase English letters ('a' to 'z').
Your task is to:
'a', 'e', 'i', 'o', or 'u') 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 letterx is the number of times it occurs in the string.
Example 1:
Input: s = "successes"
Output: 6
Explanation:
'u' (frequency 1), 'e' (frequency 2). The maximum frequency is 2.'s' (frequency 4), 'c' (frequency 2). The maximum frequency is 4.2 + 4 = 6.Example 2:
Input: s = "aeiaeia"
Output: 3
Explanation:
'a' (frequency 3), 'e' ( frequency 2), 'i' (frequency 2). The maximum frequency is 3.s. Hence, maximum consonant frequency = 0.3 + 0 = 3.
Constraints:
1 <= s.length <= 100s consists of lowercase English letters only.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.
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.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recounting for Each Character (Brute Force) | O(26 * n) | O(1) | Useful for explaining the basic idea before optimizing |
| Hash Table Frequency Count | O(n) | O(1) | General solution when counting character frequencies |
| Fixed 26-Length Array Counting | O(n) | O(1) | Best choice for lowercase alphabet strings with minimal overhead |
LeetCode#3541 Find Most Frequent Vowel and Consonant - Python • CodeJulian • 1,627 views views
Watch 9 more video solutions →Practice Find Most Frequent Vowel and Consonant with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor