[LeetCode] Find Most Frequent Vowel and Consonant

3541. Find Most Frequent Vowel and Consonant

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maxFreqSum(string s) {
unordered_map<char,int> freq[2];
unordered_set<char> vowels{'a','e','i','o','u'};
for(auto& ch : s) freq[vowels.count(ch)][ch]++;
int ma[2]{0,0};
for(int i = 0; i < 2; i++) {
for(auto& [_,v] : freq[i]) ma[i] = max(ma[i], v);
}
return ma[0] + ma[1];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/05/11/PS/LeetCode/find-most-frequent-vowel-and-consonant/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.