Algorithm


Problem Name: 1189. Maximum Number of Balloons

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 <= 104
  • text consists of lower case English letters only.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int maxNumberOfBalloons(String text) {
    Map textFrequencyMap = getFrequencyMap(text);
    Map < Character, Long> ballonFrequencyMap = getFrequencyMap("balloon");
    return ballonFrequencyMap.keySet().stream()
        .map(k -> 
            (int) (textFrequencyMap.getOrDefault(k, 0L) / ballonFrequencyMap.get(k)))
        .min(Integer::compare)
        .orElse(0);
  }

  private Map < Character, Long> getFrequencyMap(String s) {
    return s.chars()
        .mapToObj(c -> (char) c)
        .collect(Collectors.groupingBy(Function.identity(), HashMap::new, Collectors.counting()));
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "nlaebolko"

Output

x
+
cmd
1

#2 Code Example with Javascript Programming

Code - Javascript Programming


const maxNumberOfBalloons = function(text) {
  const cnt = [...text].reduce((A, ch) => {
    A[ch] = (A[ch] || 0) + 1;
    return A;
  }, {});
  const ans = Math.min(cnt['b'], cnt['a'], cnt['l'] / 2, cnt['o'] / 2, cnt['n']);
  return ans ? Math.floor(ans) : 0;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "nlaebolko"

Output

x
+
cmd
1

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def maxNumberOfBalloons(self, t: str) -> int:
          return min(t.count(c) // 'balloon'.count(c) for c in 'balon')
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "loonbalxballpoon"

Output

x
+
cmd
2

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _1189_MaximumNumberOfBalloons
    {
        public int MaxNumberOfBalloons(string text)
        {
            var counts = new int[26];
            foreach (var ch in text)
                counts[ch - 'a']++;

            var result = 0;
            while (true)
            {
                if (counts[0] > 0 && counts[1] > 0 && counts[11] > 1 && counts[13] > 0 && counts[14] > 1)
                {
                    result++;
                    counts[0]--;
                    counts[1]--;
                    counts[11] -= 2;
                    counts[13]--;
                    counts[14] -= 2;
                }
                else
                    break;
            }
            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "loonbalxballpoon"

Output

x
+
cmd
2
Advertisements

Demonstration


Previous
#1187 Leetcode Make Array Strictly Increasing Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1190 Leetcode Reverse Substrings Between Each Pair of Parentheses Solution in C, C++, Java, JavaScript, Python, C# Leetcode