当前位置:网站首页>Force buckle 1189 Maximum number of "balloons"
Force buckle 1189 Maximum number of "balloons"
2022-07-06 03:17:00 【Ruthless young Fisherman】
subject
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
Input :text = “nlaebolko”
Output :1
Input :text = “loonbalxballpoon”
Output :2
Input :text = “leetcode”
Output :0
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-number-of-balloons
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Simulation statistics
Java Realization 1: Hashtable
class Solution {
public int maxNumberOfBalloons(String text) {
Map<Character, Integer> map = new HashMap<>();
map.put('b', 0);
map.put('a', 0);
map.put('l', 0);
map.put('o', 0);
map.put('n', 0);
for (char c : text.toCharArray()) {
if (map.containsKey(c)) map.put(c, map.get(c) + 1);
}
int min = Integer.MAX_VALUE;
for (char key : map.keySet()) {
int var = 0;
if (key == 'l' || key == 'o') {
var = map.get(key) / 2;
} else {
var = map.get(key);
}
if (var < min) min = var;
}
return min;
}
}
Java Realization 2: Array
class Solution {
public int maxNumberOfBalloons(String text) {
int[] cnt = new int[5];//b a l o n
for (char c : text.toCharArray()) {
if (c == 'b') cnt[0]++;
else if (c == 'a') cnt[1]++;
else if (c == 'l') cnt[2]++;
else if (c == 'o') cnt[3]++;
else if (c == 'n') cnt[4]++;
}
cnt[2] /= 2;
cnt[3] /= 2;
int ans = Integer.MAX_VALUE;
for (int i : cnt) ans = Math.min(ans, i);
return ans;
}
}
边栏推荐
- Four logs of MySQL server layer
- Rust language -- iterators and closures
- Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
- 继承day01
- canvas切积木小游戏代码
- Codeforces 5 questions par jour (1700 chacune) - jour 6
- 深入探究指针及指针类型
- XSS challenges绕过防护策略进行 XSS 注入
- Linear regression and logistic regression
- [padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
猜你喜欢
随机推荐
Redo file corruption repair
如何做好功能测试
多态day02
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
2022工作中遇到的问题四
An article about liquid template engine
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
1.16 - 校验码
深入探究指针及指针类型
Research on cooperative control of industrial robots
Buuctf question brushing notes - [geek challenge 2019] easysql 1
建模规范:命名规范
Game theory matlab
Modeling specifications: naming conventions
张丽俊:穿透不确定性要靠四个“不变”
Arabellacpc 2019 (supplementary question)
1003 emergency (25 points), "DIJ deformation"
Rust language -- iterators and closures
Linear regression and logistic regression