commit 8c511efa9fb6e02856033dc22bf938a1a5830025
parent 6da1a3a48cb3ac0bf2e4cacbf1ecaef106dee100
Author: Jack Sangdahl <jack@pngu.org>
Date: Mon, 22 Jun 2026 08:46:28 -0600
1189. maximum number of balloons
Diffstat:
2 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/max_num_balloons.c b/max_num_balloons.c
@@ -0,0 +1,38 @@
+// 1189. Maximum Number of Balloons
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+int maxNumberOfBalloons(char *text) {
+ int freq[5] = {0};
+ char *s = "balon";
+ for (int i = 0; text[i]; i++)
+ for (int j = 0; j < 5; j++)
+ freq[j] += (text[i] == s[j]);
+ int min = freq[0];
+ min = fmin(min, freq[1]);
+ min = fmin(min, freq[2] >> 1);
+ min = fmin(min, freq[3] >> 1);
+ min = fmin(min, freq[4]);
+ return min;
+}
+
+int main() {
+ char *t1 = "nlaebolko";
+ char *t2 = "loonbalxballpoon";
+ char *t3 = "leetcode";
+ int r1 = maxNumberOfBalloons(t1);
+ int r2 = maxNumberOfBalloons(t2);
+ int r3 = maxNumberOfBalloons(t3);
+ printf("%d\n", r1);
+ assert(r1 == 1);
+ printf("%d\n", r2);
+ assert(r2 == 2);
+ printf("%d\n", r3);
+ assert(r3 == 0);
+}
diff --git a/max_num_balloons.py b/max_num_balloons.py
@@ -0,0 +1,22 @@
+# 1189. Maximum Number of Balloons
+from collections import Counter
+
+"""
+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.
+"""
+
+
+class Solution:
+ def maxNumberOfBalloons(self, text: str) -> int:
+ freq = Counter(text)
+ return min(freq["b"], freq["a"], freq["l"] >> 1, freq["o"] >> 1, freq["n"])
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxNumberOfBalloons(text="nlaebolko"))
+ print(obj.maxNumberOfBalloons(text="loonbalxballpoon"))
+ print(obj.maxNumberOfBalloons(text="leetcode"))