Problem
Qn - Find Most Frequent Vowel and Consonant
Approach
Pretty simple. One approach would be to first get the frequency dictionary then finding the max for vowels and non vowels by iterating through the frequency dictionary. The code would look nicer but that would be , we can also do it all in one iteration as seen below
Complexity
- Time:
- Space:
Code
class Solution:
def maxFreqSum(self, s: str) -> int:
vowels = {'a','e','i','o','u'}
freqDict = defaultdict(int)
vowelCount, consCount = 0, 0
for c in s:
if c in vowels:
freqDict[c] += 1
vowelCount = max(vowelCount, freqDict[c])
else:
freqDict[c] += 1
consCount = max(consCount, freqDict[c])
return vowelCount + consCount