当前位置:网站首页>[leectode 2022.2.13] maximum number of "balloons"

[leectode 2022.2.13] maximum number of "balloons"

2022-07-06 10:36:00 Procedural ape does not lose hair 2

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:

Input :text = “nlaebolko”
Output :1
Example 2:

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

java Code :

class Solution {
    public int maxNumberOfBalloons(String text) {
        int[] cnt = new int[5];
        for (int i = 0; i < text.length(); ++i) {
            char ch = text.charAt(i);
            if (ch == 'b') {
                cnt[0]++;
            } else if (ch == 'a') {
                cnt[1]++;
            } else if (ch == 'l') {
                cnt[2]++;
            } else if (ch == 'o') {
                cnt[3]++;
            } else if (ch == 'n') {
                cnt[4]++;
            }
        }
        cnt[2] /= 2;
        cnt[3] /= 2;
        return Arrays.stream(cnt).min().getAsInt();
    }
}

原网站

版权声明
本文为[Procedural ape does not lose hair 2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131709030431.html