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']
)
);
}
}
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(
min(freq[ord('b') - ord('a')], freq[ord('a') - ord('a')]),
min(
min(freq[ord('l') - ord('a')] // 2,
freq[ord('o') - ord('a')] // 2),
freq[ord('n') - ord('a')]
)
)
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(
min(freq['b' - 'a'], freq['a' - 'a']),
min(
min(freq['l' - 'a'] / 2,
freq['o' - 'a'] / 2),
freq['n' - 'a']
)
);
}
};
/**
* @param {string} text
* @return {number}
*/
var maxNumberOfBalloons = function(text) {
const freq = new Array(26).fill(0);
// Count frequency of each character
for (const ch of text) {
freq[ch.charCodeAt(0) - 97]++;
}
// "balloon" -> b=1, a=1, l=2, o=2, n=1
return Math.min(
Math.min(freq['b'.charCodeAt(0) - 97], freq['a'.charCodeAt(0) - 97]),
Math.min(
Math.min(
Math.floor(freq['l'.charCodeAt(0) - 97] / 2),
Math.floor(freq['o'.charCodeAt(0) - 97] / 2)
),
freq['n'.charCodeAt(0) - 97]
)
);
};