code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

1189 Maximum Number Of Balloons

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Given a string text, you want to use the characters of text to form as
 * many instances of the word "balloon" as possible.
 * 
 * You can use each character in text at most once. Return the maximum number
 * of instances that can be formed.
 * 
 * Example 1:
 * 
 * Input: text = "nlaebolko"
 * Output: 1
*/

function maximumNumberOfBalloons(value) {
  value = value.split('')
  let map = {}
  for (const char of value) {
    if(map.hasOwnProperty(char)){
      map[char] += 1
    } else {
      map[char] = 1
    }
  }
  return Math.floor(Math.min(map['b'], map['a'], map['l']/2, map['o']/2, map['n'])) || 0
}

test('maximum Number Of Balloons', () => {
  expect(maximumNumberOfBalloons('nlaebolko')).toEqual(1)
});

Created 2020-04-10T22:02:11+00:00 · Edit