-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathmaximumBalloons.js
More file actions
26 lines (21 loc) · 658 Bytes
/
maximumBalloons.js
File metadata and controls
26 lines (21 loc) · 658 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function maxNumBalloons(text){
let balloonObj = {'b': 1, 'a': 1, 'l':2, 'o':2, 'n': 1};
let countTextObj = {};
for (const ch of text) {
if(balloonObj[ch]) {
countTextObj[ch] = (countTextObj[ch] || 0) +1;
}
}
let minBalloons = Number.POSITIVE_INFINITY;
for (const ch in balloonObj) {
if(!countTextObj[ch]) {
return 0;
}
minBalloons = Math.min(minBalloons, Math.floor(countTextObj[ch]/balloonObj[ch]));
}
return minBalloons;
}
const text1 = "lenobxlao";
console.log(maxNumBalloons(text1));
const text2 = "lollbcolatnaboon";
console.log(maxNumBalloons(text2));