< Back to Data Structures and Algorithms
LeetCode 1189 · Strings
Maximum Number of Balloons
How many times "balloon" can be spelled from a text's letters, by the scarcest letter, l and o counting half.
Step through it
Examples
Edge cases
- l0
- o1
- o2
- n3
- b4
- a5
- l6
- x7
- b8
- a9
- l10
- l11
- p12
- o13
- o14
- n15
Step 0 of 16
Output
Running… play or step to the end.
The code
/**
* Return the maximum number of 'balloon' strings present
* @param {string} text
* @return {number}
*/
const maxNumberOfBalloons = text => {
// Edge case: No text.
if (!text) return 0
/* Create a dictionary of keys that hold the characters in 'balloon'.
Initialise each value to 0. */
const dict = {'b': 0, 'a': 0, 'l': 0, 'o': 0, 'n': 0}
// O(n) loop over every character in the string of text.
for (let i=0; i < text.length; i++) {
// The character.
const char = text[i]
// The character exists within the dictionary.
if (dict[char] !== undefined) {
// Increase the count for that character within the dictionary.
dict[char] = dict[char] + 1
}
}
/* Return the minimum value between all of the characters in the dictionary.
'l' and 'o' both occur twice in the word 'balloon'. Divide their values by 2 and round down. */
return Math.min(dict['b'],
dict['a'],
Math.floor(dict['l'] / 2),
Math.floor(dict['o'] / 2),
dict['n'])
}