LeetCode: Maximum Number of Balloons Posted on August 5, 2019July 26, 2020 by braindenny Maximum Number of Balloons Similar Problems: CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #hashmap 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 <= 10^4 text consists of lower case English letters only. Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. Solution: // https://code.dennyzhang.com/maximum-number-of-balloons // Basic Ideas: hashmap // Complexity: Time O(n), Space O(1) func min(x, y int) int { if x < y { return x } else { return y } } func maxNumberOfBalloons(text string) int { l := make([]int, 26) res := 1<<31-1 for i, _ := range text { l[text[i]-'a']++ } res = min(res, l['b'-'a']) res = min(res, l['a'-'a']) res = min(res, l['l'-'a']/2) res = min(res, l['o'-'a']/2) res = min(res, l['n'-'a']) return res } Post Views: 0