3541: Find-Most-Frequent-Vowel-and-Consonant
Easy
table of contents
Simply just iterate through the string s and keep
track of the frequency of each letter:
vector<int> frequency(26, 0);
for (char& c : s) {
++frequency[c - 'a'];
}Then, iterate through the frequency array and keep track of the maximum count for a vowel and maximum count for a consonant:
int maxVowel = 0;
int maxConsonant = 0;
for (int i = 0; i < 26; ++i) {
if (i == 0 || i == 4 || i == 'i'-'a' || i == 'o'-'a' || i == 'u'-'a') {
maxVowel = max(maxVowel, frequency[i]);
} else {
maxConsonant = max(maxConsonant, frequency[i]);
}
}
return maxVowel + maxConsonant;code
class Solution {
public:
int maxFreqSum(string s) {
vector<int> frequency(26, 0);
for (char& c : s) {
++frequency[c - 'a'];
}
int maxVowel = 0;
int maxConsonant = 0;
for (int i = 0; i < 26; ++i) {
if (i == 0 || i == 4 || i == 'i'-'a' || i == 'o'-'a' || i == 'u'-'a') {
maxVowel = max(maxVowel, frequency[i]);
} else {
maxConsonant = max(maxConsonant, frequency[i]);
}
}
return maxVowel + maxConsonant;
}
};