Find Most Frequent Vowel and Consonant

Easy
1831.

Find Most Frequent Vowel and Consonant

Find Most Frequent Vowel and Consonant is an easy coding problem on strings, hash table and counting, asked in Amazon and Google interviews.

You are given a string s of lowercase English letters. The vowels are a, e, i, o and u; every other letter is a consonant.

Find the highest frequency of any vowel in s and the highest frequency of any consonant in s, and return their sum. If s has no vowels (or no consonants), that part counts as 0.

Example 1

Input
s = "codekairo"
Output
3
Explanation
`o` is the most frequent vowel (2); every consonant occurs once. `2 + 1 = 3`.

Example 2

Input
s = "aeiaeia"
Output
3
Explanation
`a` occurs 3 times; there are no consonants, so that part is 0.

Example 3

Input
s = "rhythm"
Output
2
Explanation
No vowels; `h` occurs twice.

Constraints

  • 1 <= s.length <= 100
  • s consists only of lowercase English letters
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {number}
 */
var maxFreqSum = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find Most Frequent Vowel and Consonant — Problem & Solution

Find Most Frequent Vowel and Consonant solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Find Most Frequent Vowel and Consonant is an easy coding problem on strings, hash table and counting, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Strings, Hash Table, Counting
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a string s of lowercase English letters. The vowels are a, e, i, o and u; every other letter is a consonant.

Find the highest frequency of any vowel in s and the highest frequency of any consonant in s, and return their sum. If s has no vowels (or no consonants), that part counts as 0.

Example 1

Input: s = "codekairo"
Output: 3
Explanation: `o` is the most frequent vowel (2); every consonant occurs once. `2 + 1 = 3`.

Example 2

Input: s = "aeiaeia"
Output: 3
Explanation: `a` occurs 3 times; there are no consonants, so that part is 0.

Example 3

Input: s = "rhythm"
Output: 2
Explanation: No vowels; `h` occurs twice.

Constraints

  • 1 <= s.length <= 100
  • s consists only of lowercase English letters

Hints for Find Most Frequent Vowel and Consonant

  1. Hint 1

    Count all 26 letter frequencies in one pass.

  2. Hint 2

    Then take the maximum over the five vowels and the maximum over the 21 consonants separately, starting each at 0.

How to solve Find Most Frequent Vowel and Consonant

The two maxima are independent, so one frequency table and two separate maximum scans answer it.

Approach

  1. Count each letter's occurrences.
  2. bestVowel = largest count among a, e, i, o, u (0 if none occur).
  3. bestConsonant = largest count among the other letters (0 if none occur).
  4. Return bestVowel + bestConsonant.

Why it works

The answer is defined as the sum of two maxima over disjoint letter groups; computing each over the full frequency table gives exactly those values, with the absent-group case falling out as 0.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • y is a consonant here.
  • Add the two frequencies, not the number of distinct letters.

Reference solution

Python

def maxFreqSum(s: str) -> int:
    freq = [0] * 26
    for ch in s:
        freq[ord(ch) - 97] += 1
    vowel = consonant = 0
    for i in range(26):
        if chr(97 + i) in "aeiou":
            vowel = max(vowel, freq[i])
        else:
            consonant = max(consonant, freq[i])
    return vowel + consonant

Java

public static int maxFreqSum(String s) {
    int[] freq = new int[26];
    for (int i = 0; i < s.length(); i++) freq[s.charAt(i) - 'a']++;
    int vowel = 0, consonant = 0;
    for (int c = 0; c < 26; c++) {
        if ("aeiou".indexOf('a' + c) >= 0) vowel = Math.max(vowel, freq[c]);
        else consonant = Math.max(consonant, freq[c]);
    }
    return vowel + consonant;
}

C++

int maxFreqSum(string s) {
    int freq[26] = {0};
    for (char c : s) freq[c - 'a']++;
    int vowel = 0, consonant = 0;
    string vowels = "aeiou";
    for (int c = 0; c < 26; c++) {
        if (vowels.find((char)('a' + c)) != string::npos) vowel = max(vowel, freq[c]);
        else consonant = max(consonant, freq[c]);
    }
    return vowel + consonant;
}

JavaScript

var maxFreqSum = function(s) {
    var freq = new Array(26).fill(0);
    for (var i = 0; i < s.length; i++) freq[s.charCodeAt(i) - 97]++;
    var vowel = 0, consonant = 0;
    for (var c = 0; c < 26; c++) {
        if ("aeiou".indexOf(String.fromCharCode(97 + c)) >= 0) vowel = Math.max(vowel, freq[c]);
        else consonant = Math.max(consonant, freq[c]);
    }
    return vowel + consonant;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings · Hashing: Hash Maps and Hash Sets