Find Most Frequent Vowel and Consonant

easy hash table string counting

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.

Inputs = "successes"
Output6
Vowels: u×1, e×2 → max 2. Consonants: s×4, c×2 → max 4. Answer = 2 + 4 = 6.

def 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
}
Time: O(n) Space: O(1) (at most 26 counts)