Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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 <= 104text consists of lower case English letters only.Note: This question is the same as 2287: Rearrange Characters to Make Target String.
Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"nlaebolko"
"loonbalxballpoon"
"leetcode"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1189: Maximum Number of Balloons
class Solution {
public int maxNumberOfBalloons(String text) {
int[] cnt = new int[26];
for (int i = 0; i < text.length(); ++i) {
++cnt[text.charAt(i) - 'a'];
}
cnt['l' - 'a'] >>= 1;
cnt['o' - 'a'] >>= 1;
int ans = 1 << 30;
for (char c : "balon".toCharArray()) {
ans = Math.min(ans, cnt[c - 'a']);
}
return ans;
}
}
// Accepted solution for LeetCode #1189: Maximum Number of Balloons
func maxNumberOfBalloons(text string) int {
cnt := [26]int{}
for _, c := range text {
cnt[c-'a']++
}
cnt['l'-'a'] >>= 1
cnt['o'-'a'] >>= 1
ans := 1 << 30
for _, c := range "balon" {
if x := cnt[c-'a']; ans > x {
ans = x
}
}
return ans
}
# Accepted solution for LeetCode #1189: Maximum Number of Balloons
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
cnt = Counter(text)
cnt['o'] >>= 1
cnt['l'] >>= 1
return min(cnt[c] for c in 'balon')
// Accepted solution for LeetCode #1189: Maximum Number of Balloons
impl Solution {
pub fn max_number_of_balloons(text: String) -> i32 {
let mut arr = [0; 5];
for c in text.chars() {
match c {
'b' => {
arr[0] += 1;
}
'a' => {
arr[1] += 1;
}
'l' => {
arr[2] += 1;
}
'o' => {
arr[3] += 1;
}
'n' => {
arr[4] += 1;
}
_ => {}
}
}
arr[2] /= 2;
arr[3] /= 2;
let mut res = i32::MAX;
for num in arr {
res = res.min(num);
}
res
}
}
// Accepted solution for LeetCode #1189: Maximum Number of Balloons
function maxNumberOfBalloons(text: string): number {
const cnt = new Array(26).fill(0);
for (const c of text) {
cnt[c.charCodeAt(0) - 97]++;
}
return Math.min(cnt[0], cnt[1], cnt[11] >> 1, cnt[14] >> 1, cnt[13]);
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.