
MD ARIFUL HAQUE1189. Maximum Number of Balloons Difficulty: Easy Topics: Mid Level, Hash Table, String, Counting,...
1189. Maximum Number of Balloons
Difficulty: Easy
Topics: Mid Level, Hash Table, String, Counting, Weekly Contest 154
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:
Example 2:
Example 3:
Constraints:
1 <= text.length <= 10⁴text consists of lower case English letters only.Note: This question is the same as 2287: Rearrange Characters to Make Target String.
Hint:
Solution:
We implement a character frequency-based solution that counts occurrences of each letter in the input string and determines the maximum number of "balloon" instances by finding the limiting character ratio. The solution efficiently handles up to 10,000 characters in O(n) time and O(1) space, using the minimum ratio of available letters to required letters as the answer.
array_count_values().Let's implement this solution in PHP: 1189. Maximum Number of Balloons
<?php
/**
* @param String $text
* @return Integer
*/
function maxNumberOfBalloons(string $text): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maxNumberOfBalloons("nlaebolko") . "\n"; // Output: 1
echo maxNumberOfBalloons("loonbalxballpoon") . "\n"; // Output: 2
echo maxNumberOfBalloons("leetcode") . "\n"; // Output: 0
?>
intdiv) since we can only form complete words—partial words don't count.text is empty or missing any required letter, the function correctly returns 0 since no "balloon" can be formed.text. We iterate through the string once to count frequencies, then iterate through a constant number of required characters (5 distinct letters).Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me: