I count the frequencies of all characters in the string. Then I separate vowels (a, e, i, o, u) from consonants (all other lowercase letters). I track the maximum frequency among vowels and the maximum among consonants. If the string has no vowels or no consonants, I treat the missing side as 0. Finally, I return the sum of the two maxima.
class Solution:
def maxFreqSum(self, s: str) -> int:
vowels = set("aeiou")
freq = Counter(s)
max_vowel = 0
max_consonant = 0
for ch, count in freq.items():
if ch in vowels:
max_vowel = max(max_vowel, count)
else:
max_consonant = max(max_consonant, count)
return max_vowel + max_consonant- Time: O(n)
- Space: O(1)