Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Maximum Number of Balloons (easy)

Problem Statement

Given a string, determine the maximum number of times the word "balloon" can be formed using the characters from the string. Each character in the string can be used only once.

Examples:

  1. Example 1:

    • Input: "balloonballoon"
    • Expected Output: 2
    • Justification: The word "balloon" can be formed twice from the given string.
  2. Example 2:

    • Input: "bbaall"
    • Expected Output: 0
    • Justification: The word "balloon" cannot be formed from the given string as we are missing the character 'o' twice.
  3. Example 3:

.....

.....

.....

Like the course? Get enrolled and start learning!
D

designgurus

· a year ago

The solution code is HORRENDOUS! why would you literally hardcode all 5 lines?

class Solution: def maxNumberOfBalloons(self, text: str) -> int: charFreq = Counter(text) word = 'balloon' wordCharFreq = Counter(word) result = sys.maxsize for char in set(word): result = min(result, charFreq[char] // wordCharFreq[char]) return result