Maximum Number of Balloons

Updated on 09 June, 2025
Maximum Number of Balloons header image

Problem Statement

Given a string named text, the objective is to exercise its characters to create the word "balloon" as many times as possible. You must follow the rule that each character can only be used once per instance of "balloon". The result should be the maximum number of "balloon" instances that can be contructed using the characters from text.

Examples

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 <= 104
  • text consists of lower case English letters only.

Approach and Intuition

The essence of the problem revolves around counting occurrences of the necessary characters to form the word "balloon". Here's a step-by-step breakdown of the approach based on the given examples and constraints:

  1. Understand the composition of the word "balloon":

    • The word "balloon" consists of the characters 'b', 'a', 'l', 'o' and 'n'.
    • For each complete instance of "balloon", the counts are: one 'b', one 'a', two 'l's, two 'o's, and one 'n'.
  2. Procedure to determine the maximum number of times "balloon" can be formed:

    • First, count occurrences of 'b', 'a', 'l', 'o', and 'n' in the input string text.
    • For 'l' and 'o', since two are required for one instance of "balloon", the relevant counts need to be halved (using integer division).
    • The number of "balloon" instances can be formed will be limited by the smallest count among these necessary characters when adjusted for their required quantities in "balloon".
  3. Consider the constraints:

    • The length of text ensures that the counting operation is manageable in terms of time complexity even when text length reaches its upper limit (104 characters).
    • As text is comprised solely of lower case English letters, no checks for upper case characters or non-alphabetic characters are necessary.

By applying this approach, you efficiently count necessary characters, adjust counts for 'l' and 'o', and determine the minimum of these adjusted counts to find the maximum number of "balloon" instances that can be formed from text.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    int calculateMaxPatternFrequency(string text, string pattern) {
        int n = text.size(), m = pattern.size(), maxFreq = INT_MAX;
        int textFreq[26] = {0}, patternFreq[26] = {0};
        
        // Building frequency array for the text string
        for (int i = 0; i < n; i++) {
            textFreq[text[i] - 'a']++;
        }
        // Building frequency array for the pattern string
        for (int i = 0; i < m; i++) {
            patternFreq[pattern[i] - 'a']++;
        }
        
        // Finding the max frequency of complete patterns within text
        for (int i = 0; i < 26; i++) {
            if (patternFreq[i]) {
                maxFreq = min(maxFreq, textFreq[i] / patternFreq[i]);
            }
        }
        
        return maxFreq;
    }
    
    int maxNumberOfBalloons(string text) {
        string balloonPattern = "balloon";
        return calculateMaxPatternFrequency(text, balloonPattern);
    }
};

This summary explains how to find the maximum number of times the string "balloon" can be formed from the characters of a given text using C++. The solution utilizes a class-based approach with two primary functions for processing.

  • Start by defining a function calculateMaxPatternFrequency which determines the maximum frequency a specified pattern (in this case, "balloon") appears in the text completely.

    • This function creates two frequency arrays for both the text and the pattern, calculating how many times each character appears.
    • Iterates through the frequency array of the pattern, and for each character that appears in the pattern, it calculates the maximum number of complete patterns that can be formed by comparing the frequency of the character in the text to that in the pattern.
  • Next, define the main function maxNumberOfBalloons which:

    • Takes a string text as input.
    • Calls calculateMaxPatternFrequency, passing text and the string "balloon" as arguments.

The solution works by first building frequency arrays for both the text and the pattern, then determines the limiting character which restricts the number of complete "balloon" strings that can be created from the text. This approach efficiently calculates the result by comparing character counts and leveraging simple array operations.

java
class Solution {
    private int calculateMaxPatternOccurrences(String sourceText, String targetPattern) {
        int sourceLen = sourceText.length(), patternLen = targetPattern.length(), minOccurrences = Integer.MAX_VALUE;
        int sourceFrequency[] = new int[26];
        int patternFrequency[] = new int[26];
        
        // Populate frequency array for source text.
        for (int i = 0; i < sourceLen; i++) {
            sourceFrequency[sourceText.charAt(i) - 'a']++;
        }
        // Populate frequency array for the target pattern.
        for (int i = 0; i < patternLen; i++) {
            patternFrequency[targetPattern.charAt(i) - 'a']++;
        }
        
        // Determine the maximum number of times the pattern can appear.
        for (int i = 0; i < 26; i++) {
            if (patternFrequency[i] > 0) { // Ensure no division by zero.
                minOccurrences = Math.min(minOccurrences, sourceFrequency[i] / patternFrequency[i]);
            }
        }
        
        return minOccurrences;
    }
    
    public int maxNumberOfBalloons(String text) {
        String requiredPattern = "balloon";
        return calculateMaxPatternOccurrences(text, requiredPattern);
    }
}

The provided Java class, Solution, defines a function to determine the maximum number of times the string "balloon" can be formed from the characters of a given input string. Below is an overview of how this solution approaches the problem:

  • Determine Character Frequencies: Two frequency arrays of length 26 (one for the source text and one for the pattern "balloon") are populated to count the occurrences of each letter in the alphabet.
  • Calculate Maximum Pattern Occurrences: The solution iterates over these frequency arrays to compute the minimum number of times the pattern "balloon" can be formed. This computation is facilitated by dividing the frequency of each character in the source text by its corresponding frequency in the pattern, considering only characters that appear in the pattern.

The function maxNumberOfBalloons is the main entry to the solution which invokes another helper function, calculateMaxPatternOccurrences, with the input text and the constant pattern "balloon". This structure ensures that the solution is modular and reusable for other patterns, if necessary.

  • Since the minimum function is used to ensure the limitation by the most restrictive character, the final answer reflects the actual number of times the entire word "balloon" can be constructed from the characters of the input text without any remainder.

Comments

No comments yet.