Count how many copies of the word balloon can be built from a string's letters

Quick Overview

Given a lowercase string, find how many complete copies of the word balloon can be assembled from its letters when each character is used at most once. It tests frequency counting and careful handling of letters that the target word needs more than once.

Count how many copies of the word balloon can be built from a string's letters

Company: Wayfair

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `text`, use its characters to form as many instances of the word `"balloon"` as possible. Each character in `text` can be used at most once. Return the maximum number of instances that can be formed. ### Function Signature ```python def max_number_of_balloons(text: str) -> int: ``` ### Rules - Characters can come from any positions in `text`; their order in `text` does not matter. - Each character of `text` belongs to at most one instance. - If not even one instance can be formed, return 0. ### Constraints - `1 <= len(text) <= 10^4` - `text` consists of lowercase English letters only. ### Examples ```text Input: text = "nlaebolko" Output: 1 ``` The string has one `b`, one `a`, two `l`, two `o` and one `n`, exactly enough for one `"balloon"`. The `e` and `k` are left over. ```text Input: text = "loonbalxballpoon" Output: 2 ``` The string has two `b`, two `a`, four `l`, four `o` and two `n`, enough for two instances. The `x` and `p` are left over. ```text Input: text = "lloonan" Output: 0 ``` There is no `b` in the string, so no instance can be formed.

Overview: Given a lowercase string, find how many complete copies of the word balloon can be assembled from its letters when each character is used at most once. It tests frequency counting and careful handling of letters that the target word needs more than once.

Wayfair logo
Wayfair
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string text, use its characters to form as many instances of the word "balloon" as possible. Each character in text can be used at most once. Return the maximum number of instances that can be formed.

Function Signature

def max_number_of_balloons(text: str) -> int:

Rules

  • Characters can come from any positions in text ; their order in text does not matter.
  • Each character of text belongs to at most one instance.
  • If not even one instance can be formed, return 0.

Constraints

  • 1 <= len(text) <= 10^4
  • text consists of lowercase English letters only.

Examples

Input: text = "nlaebolko"
Output: 1

The string has one b, one a, two l, two o and one n, exactly enough for one "balloon". The e and k are left over.

Input: text = "loonbalxballpoon"
Output: 2

The string has two b, two a, four l, four o and two n, enough for two instances. The x and p are left over.

Input: text = "lloonan"
Output: 0

There is no b in the string, so no instance can be formed.