当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
How to do function test well
JS音乐在线播放插件vsPlayAudio.js
JS regular filtering and adding image prefixes in rich text
My C language learning record (blue bridge) -- on the pointer
电机控制反Park变换和反Clarke变换公式推导
11. Container with the most water
Safety science to | travel, you must read a guide
Overview of OCR character recognition methods
暑期刷题-Day3
Tidb ecological tools (backup, migration, import / export) collation
随机推荐
ArabellaCPC 2019(补题)
What is the investment value of iFLYTEK, which does not make money?
Technology sharing | what if Undo is too big
Web security SQL injection vulnerability (1)
Performance test method of bank core business system
暑期刷题-Day3
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
Sign SSL certificate as Ca
【指针训练——八道题】
Explore pointers and pointer types in depth
Redo file corruption repair
tcpdump: no suitable device found
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Daily question brushing plan-2-13 fingertip life
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
How to choose PLC and MCU?
[kubernetes series] learn the exposed application of kubernetes service security
Taobao focus map layout practice