当前位置:网站首页>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;
}
边栏推荐
- Electromagnetic radiation safety standards and detection methods
- yii2的安装之路
- ROS 、SLAM 学习 error整理
- 使网络安全威胁风险更高和成本更高的五个趋势
- MT4/MQ4L入门到精通EA教程第二课-MQL语言常用函数(二)-账户信息常用功能函数
- Composer usage record
- 最新工业界推荐系统数据集-召回排序模型原理、结构及代码实战整理分享
- Difference between KQL and Lucene
- 16.flink 自定义KeySelector
- 2022 PMP Project Management Certification Exam Registration Guide (1)
猜你喜欢

Data recovery software EasyRecovery supports recovery of all types of files

程序员的日常生活 | 每日趣闻

Educational Codeforces Round 132 (Rated for Div. 2)

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

企业面临的五大数据安全挑战

HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则

9.1-----24. Swap the nodes in the linked list in pairs

力扣刷题记录5.1-----59. 螺旋矩阵 II

gpio子系统和pinctrl子系统(上)

ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
随机推荐
mysql连接超过八小时报错
Difference between KQL and Lucene
2.1-----27. Remove elements
Summary of Database Design
UsernameAuthenticationFilter授权成功后调用AuthenticationSuccessHandler时的解析
uart_spi练习
Likou Brush Question Record--Common Functions
力扣刷题记录6.1-----203. 移除链表元素
2020.12.4 log
力扣刷题记录7.1-----707. 设计链表
gpio子系统和pinctrl子系统(中)
D. Tournament Countdown
高性能 MySQL(十二):分区表
MySQL/Oracle字符串分割
力扣刷题记录5.1-----59. 螺旋矩阵 II
年金险的安全性怎么样啊?可靠吗?
C#计算SHA1加密和base64编码
Json之JArray的使用方法
Z-Game on grid
炫酷-轮播图-走马灯