1.题目
2.思路
(1)用字典的方法
(2)今天脑子已经无法思考了,明天再补充
3.代码实现
class Solution {
public int maxNumberOfBalloons(String text) {
// 单词 "balloon" 中各字母的需求
Map<Character, Integer> required = new HashMap<>();
required.put('b', 1);
required.put('a', 1);
required.put('l', 2);
required.put('o', 2);
required.put('n', 1);
// 统计输入字符串中的字母出现次数
Map<Character, Integer> count = new HashMap<>();
for (char c : text.toCharArray()) {
count.put(c, count.getOrDefault(c, 0) + 1);
}
// 计算能拼凑多少个 "balloon"
int minBalloons = Integer.MAX_VALUE;
for (char c : required.keySet()) {
if (count.containsKey(c)) {
minBalloons = Math.min(minBalloons, count.get(c) / required.get(c));
} else {
return 0;
}
}
return minBalloons;
}
}