【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/index-pairs-of-a-string/submissions/
题目描述
You are given an array of strings words and a string chars.
A string is good if it can be formed by characters from chars (each character can only be used once).
Return the sum of lengths of all good strings in words.
Example 1:
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation:
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
Example 2:
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation:
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
Note:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
- All strings contain lowercase English letters only.
题目大意
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。
假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写时,chars 中的每个字母都只能用一次。
返回词汇表 words 中你掌握的所有单词的 长度之和。
解题方法
字典统计
这个题说字母表的每个字符只能使用一次,可以想到我们统计每个字符出现的次数,然后也判断每个单词中的字符次数进行判断就好了。
- 使用字典统计给出的字母表中每个字符出现的次数;
- 使用字典统计词汇表中每个单词中的每个字符出现的次数;
- 如果该单词中的每个字符出现的次数都小于字母表中对应字符出现的次数,那么说明可以使用字母表构成该单词。
C++代码如下:
class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
unordered_map<char, int> chars_count;
for (char c : chars) {
chars_count[c] ++;
}
int res = 0;
for (string& word : words) {
unordered_map<char, int> word_count;
for (char c: word) {
word_count[c] ++;
}
bool can_form = true;
for (auto& wc_iter : word_count) {
if (chars_count[wc_iter.first] < wc_iter.second) {
can_form = false;
break;
}
}
if (can_form) {
res += word.size();
}
}
return res;
}
};
日期
2020 年 3 月 17 日 —— 很久没有做新题了
【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)的更多相关文章
- 【Leetcode_easy】1160. Find Words That Can Be Formed by Characters
problem 1160. Find Words That Can Be Formed by Characters solution class Solution { public: int coun ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】1160. Find Words That Can Be Formed by Characters
题目如下: You are given an array of strings words and a string chars. A string is good if it can be form ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
随机推荐
- 【3】蛋白鉴定软件之Mascot
目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...
- R语言与医学统计图形-【27】ggplot2图形组合、字体、保存
ggplot2绘图系统--图形组合.字体选择.保存输出 1.图形组合 一页多图在基础包中利用par和layout函数来切分画布. ggplot2是先铺好网格背景,再进行绘图,所以要通过切分网格背景来实 ...
- cpu的性能测试
#!/bin/bash #user%加上sys%是性能的评判标准 User_sys_a=`sar -u 1 3 |tail -1 |awk '{print $3"+"$5}'|bc ...
- 分布式事务(3)---强一致性分布式事务Atomikos实战
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(4)---最终一致性方案之TCC 前面介绍强一致性分布式解决方案,这里用Atomikos框架写一个实战的dem ...
- javaSE中级篇3——集合体系(另外一种存储容器)——更新完毕
集合还是一种工具,所以它们的包都在java.util包下 1.集合的整个体系结构(是需要掌握的体系,完全体系不是这样) 对图中所说的 序和重复 这两词的说明: 序:指的是添加进去的元素和取出来的元素 ...
- 巩固javaweb的第二十天
巩固内容: 同一个页面中的多个 form 在同一个页面中可以有多个 form 如果存在多个 form,那么提交信息的时候提交哪些信息,提交给哪个文件处理,这都 与提交按钮的位置有关.如果提交按钮在第一 ...
- Linux基础命令---lynx浏览器
lynx lynx是一个字符界面的全功能www浏览器,它没有图形界面,因此占用的资源较少. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- Linux学习 - 挂载命令
一.mount 1 功能 将外设手工挂载到目标挂载点 2 语法 mount [-t 文件系统] [设备文件名] [挂载点] 3 范例 mkdir /mnt/cdrom 在/mnt下创建一个cd ...
- navigationItem的leftBarButtonItem和rightBarButtonItem隐藏
- (void)showEdit { if (不符合显示条件) { self.navigationItem.rightBarButtonItem.customView.hidden = YES; // ...
- go goroutines 使用小结
go +方法 就实现了一个并发,但由于环境不同,需要对并发的个数进行限制,限制同一时刻并发的个数,后面称此为"并发限流". 为什么要并发限流? 虽然GO M+P+G的方式号称可以轻 ...