
LeetCode 1189: Maximum Number of Balloons – Java HashMap and Frequency Counting Solution
IntroductionLeetCode 1189, Maximum Number of Balloons, is a beginner-friendly string and frequency-counting problem that teaches an important interview concept:Count only the characters that matter and determine the limiting factor.The challenge is to find how many times the word "balloon" can be formed using the characters available in a given string. Each character can be used at most once.This problem is an excellent introduction to:HashMap frequency countingCharacter frequency analysisGreedy counting techniquesFinding bottleneck charactersProblem StatementProblem Link -: Maximum Number of BalloonsGiven a string:textDetermine the maximum number of times the word:ballooncan be formed.Each character from the original string can only be used once.Return the maximum number of complete instances of the word.Example 1Inputtext = "nlaebolko"Output1ExplanationAvailable characters:b = 1a = 1l = 2o = 2n = 1Exactly one "balloon" can be formed.Example 2Inputtext = "loonbalxballpoon"Output2ExplanationThe string contains enough characters to create:balloonballoonTwo complete words can be formed.Example 3Inputtext = "leetcode"Output0ExplanationThe required characters for "balloon" are not all present.Key ObservationThe target word is:balloonCharacter requirements:CharacterRequired Countb1a1l2o2n1Notice:l appears twiceo appears twiceThese characters become important because they require more occurrences than the others.Approach 1: Simulate Building BalloonsIdeaCount occurrences of relevant characters.Continuously check whether a complete "balloon" can be formed.If possible:Increment answer.Remove required characters.Repeat until impossible.Java Solutionclass Solution { public int maxNumberOfBalloons(String text) { HashMap<Character, Integer> mp = new HashMap<>(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == 'b' || text.charAt(i) == 'a' || text.charAt(i) == 'l' || text.charAt(i) == 'o' || text.charAt(i) == 'n') { mp.put(text.charAt(i), mp.getOrDefault(text.charAt(i), 0) + 1); } } int ans = 0; for (int i = 0; i < text.length(); i++) { if (mp.containsKey('b') && mp.containsKey('a') && mp.containsKey('l') && mp.containsKey('o') && mp.containsKey('n')) { if (mp.get('b') > 0 && mp.get('a') > 0 && mp.get('l') >= 2 && mp.get('o') >= 2 && mp.get('n') > 0) { ans++; mp.put('b', mp.get('b') - 1); mp.put('a', mp.get('a') - 1); mp.put('l', mp.get('l') - 2); mp.put('o', mp.get('o') - 2); mp.put('n', mp.get('n') - 1); } } } return ans; }}Dry RunInputtext = "loonbalxballpoon"Frequency Countb = 2a = 2l = 4o = 4n = 2First BalloonConsume:b = 1a = 1l = 2o = 2n = 1Answer:1Second BalloonConsume again:b = 0a = 0l = 0o = 0n = 0Answer:2No more balloons can be formed.Final result:2Can We Optimize Further?Yes.Your solution repeatedly removes characters to form balloons.But we don't actually need simulation.Instead, we can directly calculate the maximum number of balloons.Approach 2: Frequency Division (Optimal)Key InsightSuppose frequencies are:b = 5a = 3l = 8o = 4n = 2Since:balloon = b×1 a×1 l×2 o×2 n×1Possible balloons:b → 5a → 3l → 8/2 = 4o → 4/2 = 2n → 2The smallest value determines the answer:min(5,3,4,2,2) = 2Therefore:answer =min(freq['b'],freq['a'],freq['l']/2,freq['o']/2,freq['n'])Optimized Java Solutionclass Solution { public int maxNumberOfBalloons(String text) { int[] freq = new int[26]; for (char ch : text.toCharArray()) { freq[ch - 'a']++; } return Math.min( Math.min(freq['b' - 'a'], freq['a' - 'a']), Math.min( freq['l' - 'a'] / 2, Math.min( freq['o' - 'a'] / 2, freq['n' - 'a'] ) ) ); }}Approach 3: HashMap + Minimum FormulaIf you prefer HashMaps:HashMap<Character,Integer> map = new HashMap<>();Count frequencies and apply the same minimum formula.This keeps the logic intuitive while avoiding repeated simulation.Complexity AnalysisSimulation ApproachTime ComplexityO(n)The second loop runs at most n times.Space ComplexityO(1)Only a few characters are stored.Optimal Frequency Division ApproachTime ComplexityO(n)Single traversal.Space ComplexityO(1)Fixed-size frequency array.Interview DiscussionAn interviewer may ask:Why divide l and o by 2?Because the word contains:balloonand requires:2 l's2 o'sfor every single occurrence.Therefore:available balloons from l = lCount / 2available balloons from o = oCount / 2Common MistakesForgetting Duplicate LettersMany candidates count:balonand forget that:l = 2o = 2are required.Using Total Frequency OnlyHaving:l = 1does not allow even one balloon.Simulating UnnecessarilyWhile simulation works, the minimum-frequency formula is cleaner and more efficient.Key TakeawaysThis problem is based on frequency counting.Only five characters matter:balonCharacters l and o require special handling because they appear twice.The optimal solution uses a frequency array and a minimum calculation.Understanding bottleneck resources is a common interview pattern.ConclusionLeetCode 1189: Maximum Number of Balloons is a simple but powerful frequency-counting problem. While a simulation-based approach works correctly, the most elegant solution comes from recognizing that the answer is determined by the limiting character count.Mastering this pattern will help you solve many string, HashMap, and counting-based interview questions efficiently.

















