xiqe / code-train

前端算法

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

“气球” 的最大数量

xiqe opened this issue · comments

“气球” 的最大数量

给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。

字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。

 

示例 1:

输入:text = "nlaebolko"
输出:1

示例 2:

输入:text = "loonbalxballpoon"
输出:2

示例 3:

输入:text = "leetcode"
输出:0

提示:

  • 1 <= text.length <= 10^4
  • text 全部由小写英文字母组成

reply

var maxNumberOfBalloons = function(text) {
    let rule = [['b',1],['a',1],['l',2],['o',2],['n',1]];
    let max;
    for(let i=0;i<rule.length;i++){
        let [key,ratio] = [rule[i][0],rule[i][1]];
        let _key = text.split('').reduce((s,x)=>{
            if(x==key){
                s+=1
            }
            return s;
        },0);
        if(max==undefined || _key/ratio<max){
            max = Math.floor(_key/ratio);
        }
    }
    return max
};