Find Most Frequent Vowel and Consonant
Problem
You are given a string s of lowercase English letters. Find the vowel (one of a, e, i, o, u) with the maximum frequency, and the consonant (every other letter) with the maximum frequency. Return the sum of those two maximum frequencies. If there are no vowels or no consonants, treat that side's frequency as 0.
s = "successes"6def maxFreqSum(s):
vowels = {'a', 'e', 'i', 'o', 'u'}
freq = {} # letter -> count
for ch in s:
freq[ch] = freq.get(ch, 0) + 1 # tally each letter
max_vowel = max_cons = 0
for ch, cnt in freq.items():
if ch in vowels: # vowel bucket
max_vowel = max(max_vowel, cnt)
else: # consonant bucket
max_cons = max(max_cons, cnt)
return max_vowel + max_cons # sum of the two peaks
function maxFreqSum(s) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
const freq = new Map(); // letter -> count
for (const ch of s) {
freq.set(ch, (freq.get(ch) || 0) + 1); // tally each letter
}
let maxVowel = 0, maxCons = 0;
for (const [ch, cnt] of freq) {
if (vowels.has(ch)) { // vowel bucket
maxVowel = Math.max(maxVowel, cnt);
} else { // consonant bucket
maxCons = Math.max(maxCons, cnt);
}
}
return maxVowel + maxCons; // sum of the two peaks
}
int maxFreqSum(String s) {
String vowels = "aeiou";
int[] freq = new int[26]; // count per letter
for (char ch : s.toCharArray()) {
freq[ch - 'a']++; // tally each letter
}
int maxVowel = 0, maxCons = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
int cnt = freq[ch - 'a'];
if (vowels.indexOf(ch) >= 0) { // vowel bucket
maxVowel = Math.max(maxVowel, cnt);
} else { // consonant bucket
maxCons = Math.max(maxCons, cnt);
}
}
return maxVowel + maxCons; // sum of the two peaks
}
int maxFreqSum(string s) {
string vowels = "aeiou";
int freq[26] = {0}; // count per letter
for (char ch : s) {
freq[ch - 'a']++; // tally each letter
}
int maxVowel = 0, maxCons = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
int cnt = freq[ch - 'a'];
if (vowels.find(ch) != string::npos) { // vowel bucket
maxVowel = max(maxVowel, cnt);
} else { // consonant bucket
maxCons = max(maxCons, cnt);
}
}
return maxVowel + maxCons; // sum of the two peaks
}
Explanation
This is a textbook frequency-counting problem. The two requested answers depend only on how many times each letter appears, so we first build a histogram of the string.
We walk the string once and bump freq[ch] for every character. A hash map (or a fixed 26-slot array, since the alphabet is small) gives O(1) updates, so the whole tally is linear in the length of s.
Once we have the counts, we scan the distinct letters and split them into two buckets. If a letter is one of a, e, i, o, u it competes for max_vowel; otherwise it competes for max_cons. We keep only the running maximum of each bucket — we never need the actual letter, just its peak count.
Because both maxima start at 0, the "no vowels" or "no consonants" edge cases fall out for free: an empty bucket simply contributes 0 to the sum.
Example: "successes" tallies to s×4, u×1, c×2, e×2. The vowel peak is e×2, the consonant peak is s×4, so the answer is 2 + 4 = 6.