-
Notifications
You must be signed in to change notification settings - Fork 0
/
01189. Maximum Number of Balloons.rs
38 lines (34 loc) · 1.45 KB
/
01189. Maximum Number of Balloons.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::collections::HashMap;
use std::cmp::min;
impl Solution {
// Count the number of b-a-l-o-n letters in the 'text' string & then check the minimum count of all these (divide counts of o and n by 2 since they need to appear twice)
pub fn max_number_of_balloons(text: String) -> i32 {
let mut char_count: HashMap<char, i32> = HashMap::new();
let mut max_balloons_possible: i32 = std::i32::MAX;
let mut count;
// Count the number of instances of each letter in the 'text' string
for char in text.chars() {
let mut c = char_count.entry(char).or_insert(0);
*c += 1;
}
// Check minimum of count of letters b-a-n
for char in "ban".chars() {
count = match char_count.get(&char) {
Some(&count) => count as i32,
None => 0
};
// println!("{}, {}", char, count);
max_balloons_possible = min(max_balloons_possible, count)
}
// Divide the count of letters l and o by 2 individually and then check the minimum of the two
for char in "lo".chars() {
count = match char_count.get(&char) {
Some(&count) => (count / 2) as i32,
None => 0
};
// println!("{}, {}", char, count);
max_balloons_possible = min(max_balloons_possible, count);
}
return max_balloons_possible;
}
}