Find Most Frequent Vowel and Consonant
EASYDescription
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 letterx 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 <= 100sconsists of lowercase English letters only.
Approaches
Checkout 3 different approaches to solve Find Most Frequent Vowel and Consonant. Click on different approaches to view the approach and algorithm in detail.
Brute Force with Nested Loops
This approach iterates through every letter of the alphabet. For each letter, it scans the entire input string to count its occurrences. Based on whether the letter is a vowel or a consonant, it updates the respective maximum frequency.
Algorithm
- Initialize
maxVowelFreqandmaxConsonantFreqto 0. - Create a helper function or a set to identify vowels.
- Iterate through each character
cof the alphabet from 'a' to 'z'. - For each character
c, initialize acurrentFreqcounter to 0. - Start a nested loop to iterate through the input string
s. - If a character in
smatchesc, incrementcurrentFreq. - After the inner loop finishes, check if
cis a vowel. - If
cis a vowel, updatemaxVowelFreq = Math.max(maxVowelFreq, currentFreq). - If
cis a consonant, updatemaxConsonantFreq = Math.max(maxConsonantFreq, currentFreq). - After iterating through the entire alphabet, return
maxVowelFreq + maxConsonantFreq.
The brute-force method directly tackles the problem by checking the frequency of every possible character. It involves a loop that runs 26 times (for each letter 'a' through 'z'). Inside this loop, another loop iterates through the input string s to count how many times the current letter appears. After counting, it checks if the letter is a vowel or a consonant and updates the corresponding maximum frequency found so far. While straightforward, this method is inefficient because it re-scans the input string 26 times.
class Solution {
private boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
public int findMostFrequentVowelAndConsonant(String s) {
int maxVowelFreq = 0;
int maxConsonantFreq = 0;
for (char c = 'a'; c <= 'z'; c++) {
int currentFreq = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
currentFreq++;
}
}
if (currentFreq > 0) {
if (isVowel(c)) {
maxVowelFreq = Math.max(maxVowelFreq, currentFreq);
} else {
maxConsonantFreq = Math.max(maxConsonantFreq, currentFreq);
}
}
}
return maxVowelFreq + maxConsonantFreq;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement without complex data structures.
- Uses constant extra space.
- Highly inefficient due to repeated scanning of the input string.
- The time complexity has a large constant factor (26), making it slower than single-pass approaches for the same O(N) classification.
Code Solutions
Checking out 3 solutions in different languages for Find Most Frequent Vowel and Consonant. Click on different languages to view the code.
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;
}
}
Video Solution
Watch the video walkthrough for Find Most Frequent Vowel and Consonant
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.