Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
The most frequent vowel and the most frequent consonant are independent choices. Count each category separately, take its largest individual character frequency, and add the two maxima.
Approach
Scan the lowercase string. For a character in aeiou, increment its entry in the vowel frequency map; otherwise increment its entry in the consonant map.
After the scan, find the maximum count in each map, using zero when a category is absent. Return their sum. The result uses the frequency of one most frequent character from each category, not the total number of vowels or consonants. Ties need no special treatment because only the maximum frequency is required, not the identity of the character attaining it.
class Solution:
def maxFreqSum(self, s: str) -> int:
v = defaultdict(int)
c = defaultdict(int)
for x in s:
if x in 'aeiou':
v[x] += 1
else:
c[x] += 1
return max(v.values() or [0]) + max(c.values() or [0])Complexity analysis
Let n = s.length and u be the distinct-character count. Time: O(n + u). Auxiliary space: O(u).
