当前位置:网站首页>2022.02.13 - 238. Maximum number of "balloons"

2022.02.13 - 238. Maximum number of "balloons"

2022-07-06 08:21:00 A CAI continues to work hard

1. subject

 Insert picture description here

2. Ideas

(1) Hashtable

  • Count the number of times each letter appears .

3. Code

public class Test {
    
    public static void main(String[] args) {
    
    }
}

class Solution {
    
    public int maxNumberOfBalloons(String text) {
    
        char[] chars = text.toCharArray();
        int[] count = new int[26];
        for (char c : chars) {
    
            count[c - 'a']++;
        }
        int res = Integer.MAX_VALUE;
        res = Math.min(res, count['a' - 'a']);
        res = Math.min(res, count['b' - 'a']);
        res = Math.min(res, count['l' - 'a'] >> 1);
        res = Math.min(res, count['o' - 'a'] >> 1);
        res = Math.min(res, count['n' - 'a']);
        return res;
    }
}
原网站

版权声明
本文为[A CAI continues to work hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131832390544.html