Given a string text, return the maximum number of instances of the word "balloon" that can be formed using letters in text. Each letter can only be used once.
text = "nlaebolko"1text = "loonbalxballpoon"2b×1 a×1 l×2 o×2 n×1textl and o appear twice in "balloon" → divide counts by 2min(freq[b], freq[a], freq[l]/2, freq[o]/2, freq[n])Each "balloon" consumes one b, one a, two l's, two o's, and one n. The number you can spell is limited by whichever letter runs out first — the bottleneck. Dividing l and o by 2 converts raw counts into "how many balloons" each letter supports.
Building the word over and over, and removing letters each time, is slow and messy. Counting the letters answers it directly.
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.
How many words can I build means the scarcest letter decides. Divide by how many times the word needs each letter.
Run the animation above on your own input, or watch the 1:06 video explanation of Maximum Number of Balloons with a dry run and Java code.