문제 링크 : Maximum Number of Balloons
/**
* @param {string} text
* @return {number}
*/
var maxNumberOfBalloons = function(text) {
const obj = {}
for(let s of text) {
obj[s] ? obj[s] += 1 : obj[s] = 1
}
let chk = true
let result = 0
while(chk) {
if(obj['b'] > 0 && obj['a'] > 0 && obj['l']>1 && obj['o']>1 && obj['n'] > 0) {
obj['b'] -= 1
obj['a'] -= 1
obj['l'] -= 2
obj['o'] -= 2
obj['n'] -= 1
result += 1
} else {
chk = false
}
}
return result
};
/**
* @param {string} text
* @return {number}
*/
var maxNumberOfBalloons = function(text) {
const map = {
'a': 0,
'b': 0,
'l': 0,
'n': 0,
'o': 0
};
for (let i = 0; i < text.length; i++) {
if (text[i] in map) map[text[i]]++;
}
map['l'] = Math.floor(map['l'] / 2);
map['o'] = Math.floor(map['o'] / 2);
return Math.min(...Object.values(map));
};
return Math.min(...Object.values(map));