当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
力扣刷题记录9.1-----24. 两两交换链表中的节点
etcd实现大规模服务治理应用实战
不会吧!不会吧!居然还有人不知道重绘以及回流
【剑指offer65】不适用加减乘除做加法
HMS Core分析服务智能运营6.5.1版本上线
HNUMSC-C语言第一课
MAYA发动机建模
The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
The first lesson of HNUMSC-C language
Mysql 5.7 into the pit
随机推荐
点击div内部默认文本被选中
最强分布式锁工具:Redisson
力扣刷题记录--常用功能函数
How js implements array deduplication (7 kinds)
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
D. Tournament Countdown
概率模型校准
显著性检验--学习笔记
MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions
基于JMF视频聊天
2020.10.13开发日志
年金险的安全性怎么样啊?可靠吗?
YOLOV1详解——Pytorch版
终于有人把灰度发布架构设计讲明白了
ROS2错误:不支持OpenGL 1.5 GLRenderSystem:: ci initialiseContext在C: \ \ ws \构建……
如何最大限度地减少企业受到供应链攻击的风险
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
【电商运营】不知道怎么做网站优化?这里有你需要知道的一切!
OJ:L2-012 关于堆的判断
php过滤特殊字符(仅保留中文、字母、数字、下划线)