Intuition
- Count the maximum number of times balloon amount of characters returns. This reduces to: Count the minimum amount of times each character exists in unison. Anything left over doesn’t count.
- l and o need to repeat twice, so we divide those by 2
Complexity
Runtime
, where N is the size of the text
Space
due to constant size HashMap
Code
Hashmap
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
hm = {'b': 0, 'a': 0, 'l': 0, 'o': 0, 'n': 0}
for c in text:
if c in hm:
hm[c] += 1
hm['l'] //= 2
hm['o'] //= 2
return min(hm.values())Cards
START Basic Front: Maximum Number Of Balloons Back: Count the minimum amount of times each character exists in unison. END