1189. Maximum Number of Balloons

1189. Maximum Number of Balloons #

Problem #

Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible.

You can use each character in text at most once. Return the maximum number of instances that can be formed.

Example 1:

Input: text = "nlaebolko"
Output: 1

Example 2:

Input: text = "loonbalxballpoon"
Output: 2

Example 3:

Input: text = "leetcode"
Output: 0

Constraints:

  • 1 <= text.length <= 10^4
  • text consists of lower case English letters only.

Problem Summary #

Given a string text, you need to use the letters in text to form as many instances of the word “balloon” as possible. Each letter in the string text can be used at most once. Return the maximum number of instances of the word “balloon” that can be formed.

Note:

  • 1 <= text.length <= 10^4
  • text consists entirely of lowercase English letters

Solution Approach #

  • Given a string, ask how many times the letters in the string can form the word balloon.
  • Simple problem: first count the frequency of each of the 26 letters, then take the minimum frequency among the 5 letters in balloon as the result.

Code #


package leetcode

func maxNumberOfBalloons(text string) int {
	fre := make([]int, 26)
	for _, t := range text {
		fre[t-'a']++
	}
	// The frequency of character b is the element value corresponding to array index 1
	// The frequency of character a is the element value corresponding to array index 0
	// The frequency of character l is the element value corresponding to array index 11; there are 2 l's here, so the element value needs to be divided by 2
	// The frequency of character o is the element value corresponding to array index 14; there are 2 o's here, so the element value needs to be divided by 2
	// The frequency of character n is the element value corresponding to array index 13
	return min(fre[1], min(fre[0], min(fre[11]/2, min(fre[14]/2, fre[13]))))
}


Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文