Skip to main content

< 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
text
  1. l0
  2. o1
  3. o2
  4. n3
  5. b4
  6. a5
  7. l6
  8. x7
  9. b8
  10. a9
  11. l10
  12. l11
  13. p12
  14. o13
  15. o14
  16. 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'])
}

View it on GitHub (opens in a new tab)