当前位置:网站首页>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;
}
}
边栏推荐
- . Net 6 and Net core learning notes: Important issues of net core
- Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
- canvas切积木小游戏代码
- Tomb. Weekly update of Finance (February 7 - February 13)
- Résumé des méthodes de reconnaissance des caractères ocr
- Installation and use tutorial of cobaltstrike-4.4-k8 modified version
- Performance test method of bank core business system
- #PAT#day10
- Selenium share
- What are the principles of software design (OCP)
猜你喜欢
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
IPv6 jobs
Performance analysis of user login TPS low and CPU full
three.js网页背景动画液态js特效
有没有完全自主的国产化数据库技术
Tomb. Weekly update of Finance (February 7 - February 13)
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
[concept] Web basic concept cognition
OCR文字識別方法綜述
Mysql database operation
随机推荐
1. Dynamic parameters of function: *args, **kwargs
Redis cache breakdown, cache penetration, cache avalanche
IPv6 comprehensive experiment
Tidb ecological tools (backup, migration, import / export) collation
【指针训练——八道题】
js 正则过滤和增加富文本中图片前缀
Inherit day01
MySQL advanced notes
These are not very good
The difference between sizeof and strlen in C language
1.16 - 校验码
tcpdump: no suitable device found
SD card reports an error "error -110 whilst initializing SD card
SAP ALV单元格级别设置颜色
My C language learning record (blue bridge) -- on the pointer
Differences and application scenarios between resulttype and resultmap
Add one to non negative integers in the array
NR modulation 1
Apt installation ZABBIX
有没有完全自主的国产化数据库技术