当前位置:网站首页>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;
}
}

边栏推荐
- Precautions for single chip microcomputer anti reverse connection circuit
- Redis cache breakdown, cache penetration, cache avalanche
- Some problem records of AGP gradle
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- Linear programming matlab
- 2022工作中遇到的问题四
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- 记录一下逆向任务管理器的过程
- mysqldump数据备份
- Summary of Bible story reading
猜你喜欢

svg拖动点裁剪图片js特效

ASU & OSU | model based regularized off-line meta reinforcement learning

Jenkins basic knowledge ----- detailed explanation of 03pipeline code
![[network security interview question] - how to penetrate the test file directory through](/img/48/be645442c8ff4cc5417c115963b217.jpg)
[network security interview question] - how to penetrate the test file directory through

Overview of OCR character recognition methods

MPLS experiment

【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用

Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
![[ruoyi] enable Mini navigation bar](/img/28/a8b38aecd90c8ddc98333f0e2d3eab.png)
[ruoyi] enable Mini navigation bar

2022工作中遇到的问题四
随机推荐
C # create self host webservice
Descriptor implements ORM model
My C language learning record (blue bridge) -- on the pointer
Deep parsing pointer and array written test questions
IPv6 jobs
#PAT#day10
Derivation of anti Park transform and anti Clarke transform formulas for motor control
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
SD卡報錯“error -110 whilst initialising SD card
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
4. File modification
八道超经典指针面试题(三千字详解)
SD card reports an error "error -110 whilst initializing SD card
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
IPv6 comprehensive experiment
SAP ALV颜色代码对应颜色(整理)
Safety science to | travel, you must read a guide
How to choose PLC and MCU?
Four logs of MySQL server layer
. Net 6 and Net core learning notes: Important issues of net core