#1189
Easy Algorithms Maximum number of balloons
Hash Table String Counting
60.0% acceptance
Feb 25, 2026
1874
121
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_number_of_balloons(text: String) -> i32 {
let mut cnt = [0i32; 26];
for c in text.bytes() {
cnt[(c - b'a') as usize] += 1;
}
// "balloon" = b,a,l,l,o,o,n -> b:1,a:1,l:2,o:2,n:1
let b = cnt[(b'b' - b'a') as usize];
let a = cnt[(b'a' - b'a') as usize];
let l = cnt[(b'l' - b'a') as usize] / 2;
let o = cnt[(b'o' - b'a') as usize] / 2;
let n = cnt[(b'n' - b'a') as usize];
b.min(a).min(l).min(o).min(n)
}
}