Problem

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.

Example 1

1
2
3
4
5
6
Input: s = "successes"
Output: 6
Explanation:
* The vowels are: `'u'` (frequency 1), `'e'` (frequency 2). The maximum frequency is 2.
* The consonants are: `'s'` (frequency 4), `'c'` (frequency 2). The maximum frequency is 4.
* The output is `2 + 4 = 6`.

Example 2

1
2
3
4
5
6
Input: s = "aeiaeia"
Output: 3
Explanation:
* The vowels are: `'a'` (frequency 3), `'e'` ( frequency 2), `'i'` (frequency 2). The maximum frequency is 3.
* There are no consonants in `s`. Hence, maximum consonant frequency = 0.
* The output is `3 + 0 = 3`.

Constraints

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

Examples

Solution

Method 1 – Frequency Counting with Hash Map

Intuition

We count the frequency of each vowel and consonant using a hash map. The answer is the sum of the maximum frequency among vowels and the maximum among consonants.

Approach

  1. Initialize two hash maps (or arrays): one for vowels, one for consonants.
  2. Iterate through the string:
    • If the character is a vowel, increment its count in the vowel map.
    • If it is a consonant, increment its count in the consonant map.
  3. Find the maximum value in each map (0 if empty).
  4. Return the sum of the two maxima.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    int mostFreqVowelConsonant(string s) {
        unordered_map<char, int> v, c;
        for (char ch : s) {
            if (string("aeiou").find(ch) != string::npos) v[ch]++;
            else c[ch]++;
        }
        int maxV = 0, maxC = 0;
        for (auto& p : v) maxV = max(maxV, p.second);
        for (auto& p : c) maxC = max(maxC, p.second);
        return maxV + maxC;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
func mostFreqVowelConsonant(s string) int {
    v := map[byte]int{}
    c := map[byte]int{}
    for i := 0; i < len(s); i++ {
        ch := s[i]
        if strings.ContainsRune("aeiou", rune(ch)) {
            v[ch]++
        } else {
            c[ch]++
        }
    }
    maxV, maxC := 0, 0
    for _, cnt := range v {
        if cnt > maxV { maxV = cnt }
    }
    for _, cnt := range c {
        if cnt > maxC { maxC = cnt }
    }
    return maxV + maxC
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int mostFreqVowelConsonant(String s) {
        Map<Character, Integer> v = new HashMap<>(), c = new HashMap<>();
        for (char ch : s.toCharArray()) {
            if ("aeiou".indexOf(ch) >= 0) v.put(ch, v.getOrDefault(ch, 0) + 1);
            else c.put(ch, c.getOrDefault(ch, 0) + 1);
        }
        int maxV = 0, maxC = 0;
        for (int cnt : v.values()) maxV = Math.max(maxV, cnt);
        for (int cnt : c.values()) maxC = Math.max(maxC, cnt);
        return maxV + maxC;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    fun mostFreqVowelConsonant(s: String): Int {
        val v = mutableMapOf<Char, Int>()
        val c = mutableMapOf<Char, Int>()
        for (ch in s) {
            if (ch in "aeiou") v[ch] = v.getOrDefault(ch, 0) + 1
            else c[ch] = c.getOrDefault(ch, 0) + 1
        }
        val maxV = v.values.maxOrNull() ?: 0
        val maxC = c.values.maxOrNull() ?: 0
        return maxV + maxC
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def mostFreqVowelConsonant(self, s: str) -> int:
        v = {}
        c = {}
        for ch in s:
            if ch in 'aeiou':
                v[ch] = v.get(ch, 0) + 1
            else:
                c[ch] = c.get(ch, 0) + 1
        max_v = max(v.values(), default=0)
        max_c = max(c.values(), default=0)
        return max_v + max_c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
impl Solution {
    pub fn most_freq_vowel_consonant(s: String) -> i32 {
        let mut v = std::collections::HashMap::new();
        let mut c = std::collections::HashMap::new();
        for ch in s.chars() {
            if "aeiou".contains(ch) {
                *v.entry(ch).or_insert(0) += 1;
            } else {
                *c.entry(ch).or_insert(0) += 1;
            }
        }
        let max_v = v.values().copied().max().unwrap_or(0);
        let max_c = c.values().copied().max().unwrap_or(0);
        max_v + max_c
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    mostFreqVowelConsonant(s: string): number {
        const v: Record<string, number> = {}, c: Record<string, number> = {};
        for (const ch of s) {
            if ('aeiou'.includes(ch)) v[ch] = (v[ch] || 0) + 1;
            else c[ch] = (c[ch] || 0) + 1;
        }
        const maxV = Math.max(0, ...Object.values(v));
        const maxC = Math.max(0, ...Object.values(c));
        return maxV + maxC;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of s. Each character is processed once.
  • 🧺 Space complexity: O(1), since the number of possible letters is constant.