当前位置:网站首页>【leetcode】1189. Maximum number of "balloons"

【leetcode】1189. Maximum number of "balloons"

2022-07-06 03:56:00 Chinese fir sauce_

subject :
1189. “ balloon ” Maximum number of
Give you a string text, You need to use text To piece together as many words as possible “balloon”( balloon ).

character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together “balloon”.

Example 1:
 Insert picture description here
Input :text = “nlaebolko”
Output :1
Example 2:
 Insert picture description here
Input :text = “loonbalxballpoon”
Output :2
Example 3:

Input :text = “leetcode”
Output :0

Tips :

1 <= text.length <= 10^4
text All consist of lowercase English letters

class Solution {
    
    public int maxNumberOfBalloons(String text) {
    
        char[] ch = text.toCharArray(); 
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0; i < ch.length;i++){
    
            map.put(ch[i],map.getOrDefault(ch[i],0) + 1 );
        }
        int res = 0;
        int b = map.getOrDefault('b',0);
        int a = map.getOrDefault('a',0);
        int l = map.getOrDefault('l',0) / 2;
        int o = map.getOrDefault('o',0) / 2;
        int n = map.getOrDefault('n',0);
        res = Math.min(a,b);
        res = Math.min(res,l);
        res = Math.min(res,o);
        res = Math.min(res,n);
        return res;
    }  
}
原网站

版权声明
本文为[Chinese fir sauce_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132256251243.html