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.struct Solution;
use std::collections::HashMap;
impl Solution {
fn max_number_of_balloons(text: String) -> i32 {
let mut text_count: HashMap<char, usize> = HashMap::new();
let mut ballon_count: HashMap<char, usize> = HashMap::new();
for c in "balloon".chars() {
*ballon_count.entry(c).or_default() += 1;
}
for c in text.chars() {
*text_count.entry(c).or_default() += 1;
}
let mut min = text.len();
for (c, v) in ballon_count {
min = usize::min(text_count.get(&c).unwrap_or(&0) / v, min);
}
min as i32
}
}
#[test]
fn test() {
let text = "nlaebolko".to_string();
assert_eq!(Solution::max_number_of_balloons(text), 1);
let text = "loonbalxballpoon".to_string();
assert_eq!(Solution::max_number_of_balloons(text), 2);
let text = "leetcode".to_string();
assert_eq!(Solution::max_number_of_balloons(text), 0);
}