Skip to content

3541. Find Most Frequent Vowel and Consonant

Description

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:

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:

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.

Solutions

Solution 1: Counting

We first use a hash table or an array of length \(26\), \(\textit{cnt}\), to count the frequency of each letter. Then, we iterate through this table to find the most frequent vowel and consonant, and return the sum of their frequencies.

We can use a variable \(\textit{a}\) to record the maximum frequency of vowels and another variable \(\textit{b}\) to record the maximum frequency of consonants. During the iteration, if the current letter is a vowel, we update \(\textit{a}\); otherwise, we update \(\textit{b}\).

Finally, we return \(\textit{a} + \textit{b}\).

The time complexity is \(O(n)\), where \(n\) is the length of the string. The space complexity is \(O(|\Sigma|)\), where \(|\Sigma|\) is the size of the alphabet, which is \(26\) in this case.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def maxFreqSum(self, s: str) -> int:
        cnt = Counter(s)
        a = b = 0
        for c, v in cnt.items():
            if c in "aeiou":
                a = max(a, v)
            else:
                b = max(b, v)
        return a + b
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int maxFreqSum(String s) {
        int[] cnt = new int[26];
        for (char c : s.toCharArray()) {
            ++cnt[c - 'a'];
        }
        int a = 0, b = 0;
        for (int i = 0; i < cnt.length; ++i) {
            char c = (char) (i + 'a');
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                a = Math.max(a, cnt[i]);
            } else {
                b = Math.max(b, cnt[i]);
            }
        }
        return a + b;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    int maxFreqSum(string s) {
        int cnt[26]{};
        for (char c : s) {
            ++cnt[c - 'a'];
        }
        int a = 0, b = 0;
        for (int i = 0; i < 26; ++i) {
            char c = 'a' + i;
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                a = max(a, cnt[i]);
            } else {
                b = max(b, cnt[i]);
            }
        }
        return a + b;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
func maxFreqSum(s string) int {
    cnt := [26]int{}
    for _, c := range s {
        cnt[c-'a']++
    }
    a, b := 0, 0
    for i := range cnt {
        c := byte(i + 'a')
        if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
            a = max(a, cnt[i])
        } else {
            b = max(b, cnt[i])
        }
    }
    return a + b
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
function maxFreqSum(s: string): number {
    const cnt: number[] = Array(26).fill(0);
    for (const c of s) {
        ++cnt[c.charCodeAt(0) - 97];
    }
    let [a, b] = [0, 0];
    for (let i = 0; i < 26; ++i) {
        const c = String.fromCharCode(i + 97);
        if ('aeiou'.includes(c)) {
            a = Math.max(a, cnt[i]);
        } else {
            b = Math.max(b, cnt[i]);
        }
    }
    return a + b;
}

Comments