当前位置:网站首页>Determine the maximum number of specific words in a string
Determine the maximum number of specific words in a string
2022-06-27 16:48:00 【Daily study of bald girls】
Determine the maximum number of specific words in a string
LeetCode Brush problem Day 3
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 = “oollpqwa”
Output :0
Topic link :https://leetcode-cn.com/problems/maximum-number-of-balloons/
stay LeetCode Just submit the core function part of the code .
Ideas :
"balloon" in , character l,o The number of is b,a,n Of 2 times . Statistics b,a,l,o,n After the number of , If it happens 5 individual b,5 individual a,10 individual l,10 individual o,5 individual n, be "balloon" The number of 2.
Short board effect , Take the minimum number of characters as the maximum number that can be output .
for example :b,a,l,o,n There were , Yes 2 individual b,1 individual a,10 individual l,4 individual o,3 individual n.l,o,n Although the number of , but a Only 1 individual , So there's only one ”balloon“, The maximum number is 1.
C Language complete code :
#include<stdio.h>
#include<string.h>
int maxNumberOfBalloons(char * text)
{
int zimu[6]={
0}; // Record text In the character a,b,l,o,n The number of times they appear separately , Initialize to 0
int i;
int length;
int count=0;
length=strlen(text); // Calculation text The length of
for(i=0;i<length;i++)
{
if(text[i]=='a') // Statistical characters a Number of occurrences , Save in zimu[0] in , Similarly, record b,l,o,n The number of
zimu[0]++;
if(text[i]=='b')
zimu[1]++;
if(text[i]=='l')
zimu[2]++;
if(text[i]=='o')
zimu[3]++;
if(text[i]=='n')
zimu[4]++;
}
// Statistics b,a,l,o,n After the number of , Short board effect , Take the minimum number of characters as the maximum number that can be output .
zimu[2]=(zimu[2])/2; //l,o The number of is half of the original
zimu[3]=(zimu[3])/2;
int first=zimu[0]; // Make a The number of is equal to first
for (int j = 0; j < 5; j++)
{
if(first<zimu[j]) // if a The number of characters is less than the number of other characters ,a Is the minimum
first=first;
if(first>=zimu[j])// if a The number of characters is greater than the number of other characters , Make first=zimu[j], The number of other characters is the minimum
first=zimu[j];
}
return first;
}
int main()
{
char number[1005];
scanf("%s",&number);
maxNumberOfBalloons(number);
printf("%d\n", maxNumberOfBalloons(number));
}
Running results :

边栏推荐
- Oracle概念二
- Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
- Deeply digitise, lead cloud nativity and serve more developers
- EMQ 助力青岛研博建设智慧水务平台
- 鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
- 【多线程】线程通信调度、等待集 wait() 、notify()
- Distributed session solution
- 华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
- P. Simple application of a.r.a method in Siyuan (friendly testing)
- Unity 阴影——阴影平坠(Shadow pancaking)
猜你喜欢

After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines

Source NAT address translation and server mapping web page configuration of firewall Foundation

如何提升IT电子设备效能管理

#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)

Li Chuang EDA learning notes 16: array copy and array distribution

Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)

Etcd可视化工具:Kstone部署(一),基于Helm快速部署

Distributed session solution
A large number of missing anchor text

A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
随机推荐
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
P4251 [scoi2015] small convex play matrix (still a little confused)
事务的隔离级别详解
MySQL中符号@的作用
Unity 阴影——阴影平坠(Shadow pancaking)
EMQ 助力青岛研博建设智慧水务平台
Annual comprehensive analysis of China's audio market in 2022
Yyds dry inventory brief chrome V8 engine garbage collection
Extract field year / quarter effect based on date
Weekly snapshot of substrate technology 20220411
# Cesium实现卫星在轨绕行
Use redis to automatically cancel orders within 30 minutes
QT audio playback upgrade (7)
深耕数字化,引领云原生,服务更多开发者
树莓派初步使用
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
Mobile terminal click penetration
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾