↓Skip to main content
  1. LeetCode/

LeetCode 1189: Maximum Number of Balloons

·2 mins· ·
LeetCode Easy Hash-Table String Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-10-18

  • Submissions: Runtime Error → Wrong Answer ×4 → Accepted → Runtime Error ×3 → Wrong Answer → Accepted ×2

Approach
#

balloon needs one each of b, a, n and two each of l, o. Count text (Hash Table Template 1); each letter supports cnt[c] // need[c] balloons, and the answer is the minimum over the five.

My Attempts
#

This took 12 submissions:

  • Attempt 1 assigned the letter s itself to res instead of its count C[s] (TypeError).
  • The next four filtered with C[s] < res first, comparing the raw count rather than the halved one, so a single l failed 1 < 1 and was skipped: "nolab" should be 0; they returned 1.
  • Moving to a walrus first caused syntax errors, then (new := C[s] // 2 < res) bound new to the comparison True: "ban" returned True. Moving the parenthesis to (new := C[s] // 2) < res passed.

Solution
#

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        cnt = Counter(text)
        need = Counter("balloon")
        return min(cnt[c] // need[c] for c in need)

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Divide by the needed amount first, then take the minimum; do not filter with raw counts.
  • Parenthesize the walrus: (new := x // 2) < res, otherwise new gets the comparison result.

Interview Explanation
#

I count text, divide each letter’s count by how many balloon needs (two for l and o), and take the minimum of the five. Time O(n).

Related

LeetCode 884: Uncommon Words from Two Sentences
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 884 note: count words across both sentences together; those appearing exactly once are the answer
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 383: Ransom Note
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 383 note: every character’s demand in ransomNote must not exceed its supply in magazine; an empty Counter difference means yes
LeetCode 387: First Unique Character in a String
·1 min
LeetCode Easy Hash-Table String Queue Counting
LeetCode 387 note: count first, then scan left to right for the first character with count 1
LeetCode 1160: Find Words That Can Be Formed by Characters
·1 min
LeetCode Easy Array Hash-Table String Counting
LeetCode 1160 note: a word is formable when none of its character counts exceeds chars’; sum the lengths of such words
LeetCode 1128: Number of Equivalent Domino Pairs
·2 mins
LeetCode Easy Array Hash-Table Counting
LeetCode 1128 note: normalize each domino to (small, large) as a key and count; a key with c dominoes yields c(c - 1) / 2 pairs