↓Skip to main content
  1. LeetCode/

LeetCode 3541: Find Most Frequent Vowel and Consonant

·1 min· ·
LeetCode Easy Hash-Table String Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-05-10

  • Submissions: Accepted (first try)

Approach
#

Count characters with Counter, then take the maximum within vowels and within consonants (String Template 4). A class with no characters counts as 0, via max(..., default=0).

My Solution
#

I originally sorted the Counter by count before scanning it; the sort is unnecessary, just take the maximums:

class Solution:
    def maxFreqSum(self, s: str) -> int:
        vowels = set("aeiou")
        cnt = Counter(s)
        best_v = max((v for c, v in cnt.items() if c in vowels), default=0)
        best_c = max((v for c, v in cnt.items() if c not in vowels), default=0)
        return best_v + best_c

Complexity
#

Time O(n), space O(1) (at most 26 characters).

Mistakes to Avoid
#

  • With no characters in a class, max on an empty sequence raises; pass default=0.

Interview Explanation
#

I count each character, take the maximum among vowels and among consonants, and return their sum, using 0 for a missing class. Time O(n).

Related

LeetCode 1160: Find Words That Can Be Formed by Characters
·1 min
LeetCode Easy Array Hash-Table String Counting
LeetCode 1160 note: a word is formable when none of its character counts exceeds chars’; sum the lengths of such words
LeetCode 1189: Maximum Number of Balloons
·2 mins
LeetCode Easy Hash-Table String Counting
LeetCode 1189 note: each letter supports count // needed balloons, with l and o needed twice; take the minimum
LeetCode 884: Uncommon Words from Two Sentences
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 884 note: count words across both sentences together; those appearing exactly once are the answer
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 383: Ransom Note
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 383 note: every character’s demand in ransomNote must not exceed its supply in magazine; an empty Counter difference means yes
LeetCode 387: First Unique Character in a String
·1 min
LeetCode Easy Hash-Table String Queue Counting
LeetCode 387 note: count first, then scan left to right for the first character with count 1