/**
* Problem: LeetCode 1189 - Maximum Number of Balloons
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int maxNumberOfBalloons(String text) {
int[] freq = new int[26];
// Count frequency of each character
for (char ch : text.toCharArray()) {
freq[ch - 'a']++;
}
// "balloon" -> b=1, a=1, l=2, o=2, n=1
return Math.min(
Math.min(freq['b' - 'a'], freq['a' - 'a']),
Math.min(
Math.min(freq['l' - 'a'] / 2, freq['o' - 'a'] / 2),
freq['n' - 'a']
)
);
}
}
"""
Problem: LeetCode 1189 - Maximum Number of Balloons
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
freq = [0] * 26
# Count frequency of each character
for ch in text:
freq[ord(ch) - ord('a')] += 1
# "balloon" -> b=1, a=1, l=2, o=2, n=1
return min(
freq[ord('b') - ord('a')],
freq[ord('a') - ord('a')],
freq[ord('l') - ord('a')] // 2,
freq[ord('o') - ord('a')] // 2,
freq[ord('n') - ord('a')]
)
/**
* Problem: LeetCode 1189 - Maximum Number of Balloons
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 1189 - Maximum Number of Balloons
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int maxNumberOfBalloons(string text) {
vector<int> freq(26, 0);
// Count frequency of each character
for (char ch : text) {
freq[ch - 'a']++;
}
// "balloon" -> b=1, a=1, l=2, o=2, n=1
return min({
freq['b' - 'a'],
freq['a' - 'a'],
freq['l' - 'a'] / 2,
freq['o' - 'a'] / 2,
freq['n' - 'a']
});
}
};
/**
* Problem: LeetCode 1189 - Maximum Number of Balloons
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var maxNumberOfBalloons = function(text) {
const freq = new Int32Array(26);
// Count frequency of each character
for (let i = 0; i < text.length; i++) {
freq[text.charCodeAt(i) - 97]++;
}
// "balloon" -> b=1, a=1, l=2, o=2, n=1
const b = freq['b'.charCodeAt(0) - 97];
const a = freq['a'.charCodeAt(0) - 97];
const l = Math.floor(freq['l'.charCodeAt(0) - 97] / 2);
const o = Math.floor(freq['o'.charCodeAt(0) - 97] / 2);
const n = freq['n'.charCodeAt(0) - 97];
return Math.min(b, a, l, o, n);
};