Skip to content

Commit 82ab43d

Browse files
committed
completed 1189. maximum number of balloons in go, python and rust
1 parent 9e60213 commit 82ab43d

File tree

8 files changed

+180
-0
lines changed

8 files changed

+180
-0
lines changed

go/maximumNumberOfBallons/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 1189. Maximum Number of Balloons
2+
3+
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
4+
5+
You can use each character in text at most once. Return the maximum number of instances that can be formed.
6+
7+
8+
9+
Example 1:
10+
11+
Input: text = "nlaebolko"
12+
Output: 1
13+
14+
15+
Example 2:
16+
17+
Input: text = "loonbalxballpoon"
18+
Output: 2
19+
20+
21+
Example 3:
22+
23+
Input: text = "leetcode"
24+
Output: 0
25+
26+
27+
Constraints:
28+
29+
1 <= text.length <= 104
30+
text consists of lower case English letters only.

go/maximumNumberOfBallons/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module maximumNumberOfBalloons
2+
3+
go 1.23.2

go/maximumNumberOfBallons/solution.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package solution
2+
3+
func maxNumberOfBalloons(text string) int {
4+
counts := make(map[rune]int)
5+
6+
for _, r := range text {
7+
counts[r]++
8+
}
9+
10+
count := counts['b']
11+
count = min(count, counts['a'])
12+
count = min(count, counts['n'])
13+
count = min(count, counts['l']/2)
14+
count = min(count, counts['o']/2)
15+
16+
return count
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package solution
2+
3+
import "testing"
4+
5+
func TestMaxNumberOfBalloons(t *testing.T) {
6+
testCases := []struct {
7+
name string
8+
text string
9+
want int
10+
}{
11+
{
12+
name: "test case one",
13+
text: "nlaebolko",
14+
want: 1,
15+
},
16+
{
17+
name: "test case two",
18+
text: "loonbalxballpoon",
19+
want: 2,
20+
},
21+
{
22+
name: "test case three",
23+
text: "leetcode",
24+
want: 0,
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
got := maxNumberOfBalloons(tc.text)
31+
32+
if got != tc.want {
33+
t.Errorf("got %d, wanted %d", got, tc.want)
34+
}
35+
})
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxNumberOfBalloons(self, text: str) -> int:
3+
counts = {}
4+
5+
for char in text:
6+
counts[char] = counts.get(char, 0) + 1
7+
8+
count = counts.get('b', 0)
9+
count = min(count, counts.get('a', 0))
10+
count = min(count, counts.get('n', 0))
11+
count = min(count, counts.get('l', 0) // 2)
12+
count = min(count, counts.get('o', 0) // 2)
13+
14+
return count
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from solution import Solution
3+
4+
class TestMaxNumberOfBalloons(unittest.TestCase):
5+
solution = Solution()
6+
7+
def test_case_one(self):
8+
text = "nlaebolko"
9+
expected = 1
10+
result = self.solution.maxNumberOfBalloons(text)
11+
self.assertEqual(result, expected)
12+
13+
def test_case_two(self):
14+
text = "loonbalxballpoon"
15+
expected = 2
16+
result = self.solution.maxNumberOfBalloons(text)
17+
self.assertEqual(result, expected)
18+
19+
def test_case_three(self):
20+
text = "leetcode"
21+
expected = 0
22+
result = self.solution.maxNumberOfBalloons(text)
23+
self.assertEqual(result, expected)

rs/max_number_of_balloons/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "max_number_of_balloons"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

rs/max_number_of_balloons/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::HashMap;
2+
3+
struct Solution {}
4+
5+
impl Solution {
6+
pub fn max_number_of_balloons(text: String) -> i32 {
7+
let mut counts = HashMap::new();
8+
9+
for c in text.chars() {
10+
*counts.entry(c).or_insert(0) += 1;
11+
}
12+
13+
let mut count = *counts.get(&'b').unwrap_or(&0);
14+
count = count.min(*counts.get(&'a').unwrap_or(&0));
15+
count = count.min(*counts.get(&'n').unwrap_or(&0));
16+
count = count.min(*counts.get(&'l').unwrap_or(&0) / 2);
17+
count = count.min(*counts.get(&'o').unwrap_or(&0) / 2 );
18+
19+
count
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn test_case_one() {
29+
let expected = 1;
30+
let text = String::from("nlaebolko");
31+
let result = Solution::max_number_of_balloons(text);
32+
assert_eq!(result, expected);
33+
}
34+
35+
#[test]
36+
fn test_case_two() {
37+
let expected = 2;
38+
let text = String::from("loonbalxballpoon");
39+
let result = Solution::max_number_of_balloons(text);
40+
assert_eq!(result, expected);
41+
}
42+
43+
#[test]
44+
fn test_case_three() {
45+
let expected = 0;
46+
let text = String::from("leetcode");
47+
let result = Solution::max_number_of_balloons(text);
48+
assert_eq!(result, expected);
49+
}
50+
}

0 commit comments

Comments
 (0)