Skip to content

Commit 0efa425

Browse files
committedJan 16, 2020
Add solution #1189
1 parent 8e10066 commit 0efa425

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1189. Maximum Number of Balloons
3+
* https://leetcode.com/problems/maximum-number-of-balloons/
4+
* Difficulty: Easy
5+
*
6+
* Given a string text, you want to use the characters of text
7+
* to form as many instances of the word "balloon" as possible.
8+
*
9+
* You can use each character in text at most once. Return the
10+
* maximum number of instances that can be formed.
11+
*/
12+
13+
/**
14+
* @param {string} text
15+
* @return {number}
16+
*/
17+
var maxNumberOfBalloons = function(text, word = 'balloon', i = 0, count = 0) {
18+
const map = text.split('').reduce((o, k) => ({...o, [k]: o[k] ? ++o[k] : 1}), {});
19+
while (true) {
20+
const char = word[i++ % word.length];
21+
if (!map[char] || map[char]-- === 0) break;
22+
if (i % word.length === 0) count++;
23+
}
24+
return count;
25+
};

0 commit comments

Comments
 (0)