DSA AnimatorREVISION VIDEOS
← Back to problems
New

Maximum Number of Balloons — LeetCode 1189 video explanation

Using the letters of a text, at most once each, how many times can you form the word balloon?

Loading videos…

DSA Animator › DSA Revision › Strings › Maximum Number of Balloons

Key moments in the Maximum Number of Balloons video

0:04Problem0:10Example0:14Brute force0:21Key idea0:32Dry run0:46Code0:57Takeaway

What you will learn

Remember: how many words can I build means the scarcest letter decides. Divide by how many times the word needs each letter. The video solves Maximum Number of Balloons (LeetCode 1189, Easy) with the Frequency Count pattern from the Strings topic, and you can replay every step in the interactive Maximum Number of Balloons animation.

Maximum Number of Balloons video transcript

Problem

Using the letters of a text, at most once each, how many times can you form the word balloon?

Example

For loon bal x ball poon, you can form balloon 2 times.

Brute force

Building the word over and over, and removing letters each time, is slow and messy. Counting the letters answers it directly.

Key idea

Count each letter of the text. Balloon needs one b, one a, two l, two o and one n. So divide the counts of l and o by 2, and the answer is the smallest result.

Dry run

The counts are b 2, a 2, l 4, o 4 and n 2. b gives 2, and a gives 2. l gives 4 divided by 2, which is 2, and o also gives 2. n gives 2. The smallest of these is 2, so the answer is 2.

Code

In code, count all 26 letters. Return the minimum of b, a, l divided by 2, o divided by 2, and n. That's O of n time and O of 1 space.

Takeaway

Remember: how many words can I build means the scarcest letter decides. Divide by how many times the word needs each letter.

More Strings revision videos

← All 185 DSA revision videos