Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"successes"
"aeiaeia"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3541: Find Most Frequent Vowel and Consonant
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;
}
}
// Accepted solution for LeetCode #3541: Find Most Frequent Vowel and Consonant
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
}
# Accepted solution for LeetCode #3541: Find Most Frequent Vowel and Consonant
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
// Accepted solution for LeetCode #3541: Find Most Frequent Vowel and Consonant
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
}
}
// Accepted solution for LeetCode #3541: Find Most Frequent Vowel and Consonant
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;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.