当前位置:网站首页>1160. 拼写单词
1160. 拼写单词
2022-08-09 02:22:00 【Mr Gao】
1160. 拼写单词
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。
假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写(指拼写词汇表中的一个单词)时,chars 中的每个字母都只能用一次。
返回词汇表 words 中你掌握的所有单词的 长度之和。
示例 1:
输入:words = [“cat”,“bt”,“hat”,“tree”], chars = “atach”
输出:6
解释:
可以形成字符串 “cat” 和 “hat”,所以答案是 3 + 3 = 6。
示例 2:
输入:words = [“hello”,“world”,“leetcode”], chars = “welldonehoneyr”
输出:10
解释:
可以形成字符串 “hello” 和 “world”,所以答案是 5 + 5 = 10。
解题代码如下:
bool include(int a[],char *b){
int az[26];
for(int i=0;i<26;i++){
az[i]=a[i];
}
for(int i=0;b[i]!='\0';i++){
az[b[i]-'a']--;
if( az[b[i]-'a']<0){
return false;
}
}
return true;
}
int countCharacters(char ** words, int wordsSize, char * chars){
int *r=(int *)malloc(sizeof(int)*26);
for(int i=0;i<26;i++){
r[i]=0;
}
for(int i=0;chars[i]!='\0';i++){
r[chars[i]-'a']++;
}
int len=0;
for(int i=0;i<wordsSize;i++){
if(include(r,words[i])){
len=len+strlen(words[i]);
}
// printf("%s ",words[i]);
}
return len;
}
边栏推荐
- gpio子系统和pinctrl子系统(上)
- 2022/8/8 比赛思维+状压dp
- 【剑指offer65】不适用加减乘除做加法
- HNUMSC-C语言第一课
- The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
- 显著性检验--学习笔记
- Several ways to use JS to achieve array flattening
- MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
- 高性能 MySQL(十二):分区表
- 2022护眼产品展,北京眼健康展,眼科医学展,近视矫正设备展
猜你喜欢

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules

Etcd realize large-scale application service management of actual combat

配置文件的读取-TOML

js实现数组去重的方式(7种)

10.1-----19. Delete the Nth node from the bottom of the linked list

力扣刷题记录2.1-----27. 移除元素

Data recovery software EasyRecovery supports recovery of all types of files

【izpack】使用izpack为你的程序提供安装程序封装

2022杭电多校第五场1007(生成函数+启发式合并+ntt)

MT4/MQL4入门到精通EA教程第一课-MQL语言常用函数(一)OrderSend()函数
随机推荐
【HNUMSC】C language second lecture
增额终身寿险哪家最好呢?真的安全吗?
ApiFile配置环境
2.1-----27. Remove elements
USB 触摸在竖屏时校准
spdlog日志库的封装使用
Z-Game on grid(牛客多校赛)
MT4/MQL4入门到精通EA课程第二课-常用的功能函数
2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
C#计算SHA1加密和base64编码
Mysql 5.7 into the pit
数据库设计的总结
mysql 5.7 入坑
项目经理VS产品经理,二者到底有何不同?
Composer usage record
概率模型校准
帮助安全红队取得成功的11条建议
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
Apache站点下载大文件自动中断或者文件不完整