Given a string, count the frequencies of vowels and consonants. Find the maximum frequency among vowels and the maximum frequency among consonants. Return the sum of these two maximum frequencies.
Example Input: s = “hello”
Step-by-step execution:
class Solution {
public int maxFreqSum(String s) {
boolean[] vowels = new boolean[26];
for (char ch : new char[] { 'a', 'e', 'i', 'o', 'u' }) {
vowels[ch - 'a'] = true;
}
int[] vowFreq = new int[26];
int[] conFreq = new int[26];
for (char ch : s.toCharArray()) {
int index = ch - 'a';
if (vowels[index]) {
vowFreq[index]++;
} else {
conFreq[index]++;
}
}
int maxVow = 0;
int maxCon = 0;
for (int i = 0; i < 26; i++) {
maxVow = Math.max(maxVow, vowFreq[i]);
maxCon = Math.max(maxCon, conFreq[i]);
}
return maxVow + maxCon;
}
}
Example Input: s = “hello”
Step-by-step execution:
class Solution {
HashMap<Character, Integer> vMap = new HashMap<>();
HashMap<Character, Integer> cMap = new HashMap<>();
public int maxFreqSum(String s) {
for (char ch : s.toCharArray()) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vMap.put(ch, vMap.getOrDefault(ch, 0) + 1);
} else {
cMap.put(ch, cMap.getOrDefault(ch, 0) + 1);
}
}
int maxV = 0;
// find max vowel frequency
for (int val : vMap.values()) {
if (val > maxV) {
maxV = val;
}
}
int maxC = 0;
// find max consonant frequency
for (int val : cMap.values()) {
if (val > maxC) {
maxC = val;
}
}
return maxV + maxC;
}
}