Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1189.html

1189. Maximum Number of Balloons (Easy)

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.

Companies:
Wayfair

Related Topics:
Hash Table, String

Solution 1.

We can also use undered_map which makes the code simpler. But I found it’s slower with unordered_map.

// OJ: https://leetcode.com/problems/maximum-number-of-balloons/
// Time: O(N)
// Space: O(1)
class Solution {
public:
    int maxNumberOfBalloons(string text) {
        int cnt[26] = {0};
        for (char c : text) cnt[c - 'a']++;
        return min({ cnt['b' - 'a'], cnt['a' - 'a'], cnt['n' - 'a'],
                     cnt['l' - 'a'] / 2, cnt['o' - 'a'] / 2 });
    }
};
  • class Solution {
        public int maxNumberOfBalloons(String text) {
            int[] count = new int[26];
            char[] array = text.toCharArray();
            for (char c : array)
                count[c - 'a']++;
            int max = Integer.MAX_VALUE;
            max = Math.min(max, count['b' - 'a']);
            max = Math.min(max, count['a' - 'a']);
            max = Math.min(max, count['l' - 'a'] / 2);
            max = Math.min(max, count['o' - 'a'] / 2);
            max = Math.min(max, count['n' - 'a']);
            return max;
        }
    }
    
    ############
    
    class Solution {
        public int maxNumberOfBalloons(String text) {
            int[] counter = new int[26];
            for (char c : text.toCharArray()) {
                ++counter[c - 'a'];
            }
            counter['l' - 'a'] >>= 1;
            counter['o' - 'a'] >>= 1;
            int ans = 10000;
            for (char c : "balon".toCharArray()) {
                ans = Math.min(ans, counter[c - 'a']);
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/maximum-number-of-balloons/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    public:
        int maxNumberOfBalloons(string text) {
            int cnt[26] = {0};
            for (char c : text) cnt[c - 'a']++;
            return min({ cnt['b' - 'a'], cnt['a' - 'a'], cnt['n' - 'a'],
                         cnt['l' - 'a'] / 2, cnt['o' - 'a'] / 2 });
        }
    };
    
  • class Solution:
        def maxNumberOfBalloons(self, text: str) -> int:
            counter = Counter(text)
            counter['l'] >>= 1
            counter['o'] >>= 1
            return min(counter[c] for c in 'balon')
    
    ############
    
    # 1189. Maximum Number of Balloons
    # https://leetcode.com/problems/maximum-number-of-balloons/
    
    class Solution:
        def maxNumberOfBalloons(self, text: str) -> int:
            cnt = collections.Counter(text)
            
            return min(cnt["b"], cnt["a"], cnt["l"] // 2, cnt["o"] // 2, cnt["n"])
    
    
  • func maxNumberOfBalloons(text string) int {
    	counter := make([]int, 26)
    	for i := range text {
    		counter[text[i]-'a']++
    	}
    	counter['l'-'a'] >>= 1
    	counter['o'-'a'] >>= 1
    	ans := 10000
    	t := "balon"
    	for i := range t {
    		ans = min(ans, counter[t[i]-'a'])
    	}
    	return ans
    }
    
    func min(a, b int) int {
    	if a < b {
    		return a
    	}
    	return b
    }
    
  • function maxNumberOfBalloons(text: string): number {
        let targets: Set<string> = new Set('balloon'.split(''));
        let cnt = new Array(126).fill(0);
        for (let char of text) {
            if (targets.has(char)) {
                cnt[char.charCodeAt(0)]++;
            }
        }
        cnt['l'.charCodeAt(0)] >>= 1;
        cnt['o'.charCodeAt(0)] >>= 1;
        let ans = Number.MAX_SAFE_INTEGER;
        for (let char of targets) {
            ans = Math.min(cnt[char.charCodeAt(0)], ans);
        }
        return ans;
    }
    
    
  • 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
        }
    }
    
    
  • class Solution {
        /**
         * @param String $text
         * @return Integer
         */
        function maxNumberOfBalloons($text) {
            $cnt1 = $cnt2 = $cnt3 = $cnt4 = $cnt5 = 0;
            for ($i = 0; $i < strlen($text); $i++) {
                if ($text[$i] == "b") $cnt1 += 1;
                else if ($text[$i] == "a") $cnt2 += 1;
                else if ($text[$i] == "l") $cnt3 += 1;
                else if ($text[$i] == "o") $cnt4 += 1;
                else if ($text[$i] == "n") $cnt5 += 1;
            }
            $cnt3 = floor($cnt3 / 2);
            $cnt4 = floor($cnt4 / 2);
            return min($cnt1, $cnt2, $cnt3, $cnt4, $cnt5);
        }
    }
    

All Problems

All Solutions