
Leetcode Problem 1189: Maximum Number of Balloons
Given a string text, return the maximum number of times the word "balloon" can be formed using characters from text. Each character from text may be used at most once per instance.
Constraints:
function maxNumberOfBalloons(text: string): number {
const balloonLetterCount: Record<string, number> = {
b: 0,
a: 0,
l: 0,
o: 0,
n: 0
}
for (let i = 0; i < text.length; i++) {
if (balloonLetterCount[text.charAt(i)] !== undefined) {
balloonLetterCount[text.charAt(i)]++
}
}
balloonLetterCount['l'] = Math.floor(balloonLetterCount['l'] / 2)
balloonLetterCount['o'] = Math.floor(balloonLetterCount['o'] / 2)
return Math.min(
balloonLetterCount['b'],
balloonLetterCount['a'],
balloonLetterCount['l'],
balloonLetterCount['o'],
balloonLetterCount['n']
)
}
console.log(maxNumberOfBalloons("loonbalxballpoon"))