3541. Find Most Frequent Vowel and Consonant LeetCode Solution

In this guide, you will get 3541. Find Most Frequent Vowel and Consonant LeetCode Solution with the best time and space complexity. The solution to Find Most Frequent Vowel and Consonant problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Most Frequent Vowel and Consonant solution in C++
  4. Find Most Frequent Vowel and Consonant solution in Java
  5. Find Most Frequent Vowel and Consonant solution in Python
  6. Additional Resources
3541. Find Most Frequent Vowel and Consonant LeetCode Solution image

Problem Statement of Find Most Frequent Vowel and Consonant

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26) = O(1)

3541. Find Most Frequent Vowel and Consonant LeetCode Solution in C++

class Solution {
 public:
  int maxFreqSum(string s) {
    vector<int> count(26);
    int maxVowel = 0;
    int maxConsonant = 0;

    for (const char c : s)
      ++count[c - 'a'];

    for (const char c : s)
      if (isVowel(c))
        maxVowel = max(maxVowel, count[c - 'a']);
      else
        maxConsonant = max(maxConsonant, count[c - 'a']);

    return maxVowel + maxConsonant;
  }

 private:
  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiou";
    return kVowels.find(c) != string_view::npos;
  }
};
/* code provided by PROGIEZ */

3541. Find Most Frequent Vowel and Consonant LeetCode Solution in Java

class Solution {
  public int maxFreqSum(String s) {
    int[] count = new int[26];
    int maxVowel = 0;
    int maxConsonant = 0;

    for (final char c : s.toCharArray())
      ++count[c - 'a'];

    for (final char c : s.toCharArray())
      if (isVowel(c))
        maxVowel = Math.max(maxVowel, count[c - 'a']);
      else
        maxConsonant = Math.max(maxConsonant, count[c - 'a']);

    return maxVowel + maxConsonant;
  }

  private boolean isVowel(char c) {
    return "aeiou".indexOf(c) != -1;
  }
}
// code provided by PROGIEZ

3541. Find Most Frequent Vowel and Consonant LeetCode Solution in Python

class Solution:
  def maxFreqSum(self, s: str) -> int:
    VOWELS = 'aeiou'
    count = collections.Counter(s)
    maxVowel = max((count[c] for c in VOWELS if c in count), default=0)
    maxConsonant = max((count[c] for c in count if c not in VOWELS), default=0)
    return maxVowel + maxConsonant
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.