题目地址: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:

  1. Input: words = ["cat","bt","hat","tree"], chars = "atach"
  2. Output: 6
  3. Explanation:
  4. The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

Example 2:

  1. Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
  2. Output: 10
  3. Explanation:
  4. The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.

Note:

  1. 1 <= words.length <= 1000
  2. 1 <= words[i].length, chars.length <= 100
  3. All strings contain lowercase English letters only.

题目大意

给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。

注意:每次拼写时,chars 中的每个字母都只能用一次。

返回词汇表 words 中你掌握的所有单词的 长度之和。

解题方法

字典统计

这个题说字母表的每个字符只能使用一次,可以想到我们统计每个字符出现的次数,然后也判断每个单词中的字符次数进行判断就好了。

  1. 使用字典统计给出的字母表中每个字符出现的次数;
  2. 使用字典统计词汇表中每个单词中的每个字符出现的次数;
  3. 如果该单词中的每个字符出现的次数都小于字母表中对应字符出现的次数,那么说明可以使用字母表构成该单词。

C++代码如下:

  1. class Solution {
  2. public:
  3. int countCharacters(vector<string>& words, string chars) {
  4. unordered_map<char, int> chars_count;
  5. for (char c : chars) {
  6. chars_count[c] ++;
  7. }
  8. int res = 0;
  9. for (string& word : words) {
  10. unordered_map<char, int> word_count;
  11. for (char c: word) {
  12. word_count[c] ++;
  13. }
  14. bool can_form = true;
  15. for (auto& wc_iter : word_count) {
  16. if (chars_count[wc_iter.first] < wc_iter.second) {
  17. can_form = false;
  18. break;
  19. }
  20. }
  21. if (can_form) {
  22. res += word.size();
  23. }
  24. }
  25. return res;
  26. }
  27. };

日期

2020 年 3 月 17 日 —— 很久没有做新题了

【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)的更多相关文章

  1. 【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 ...

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  5. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  6. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  7. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  8. 【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 ...

  9. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

随机推荐

  1. 35-Remove Element

    Remove Element My Submissions QuestionEditorial Solution Total Accepted: 115367 Total Submissions: 3 ...

  2. EXCEL-COUNTIF()统计符合区间上的值个数

    =COUNTIF(D9:D21465,"<-0.2")+COUNTIF(D9:D21465,">0.2")  #计算<-0.2或者>0. ...

  3. 深入了解scanf() getchar()和gets()等函数之间的区别

    scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些函数除了问题,却找不出其中的原因.下面先看一个很简单的程序: 程序1 ...

  4. SPI详解2

    串行外设接口 (SPI) 总线是一种运行于全双工模式下的同步串行数据链路.用于在单个主节点和一个或多个从节点之间交换数据. SPI 总线实施简单,仅使用四条数据信号线和控制信号线(请参见图 1). 图 ...

  5. 巩固javawbe第二天

    巩固内容: <!DOCTYPE> 声明 <!DOCTYPE>声明有助于浏览器中正确显示网页. 网络上有很多不同的文件,如果能够正确声明HTML的版本,浏览器就能正确显示网页内容 ...

  6. A Child's History of England.48

    A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...

  7. day12 查找文件

    day12 查找文件 find命令:查找文件 find命令:在linux系统中,按照我们的要求去查询文件. 格式: find [查询的路径] [匹配模式] [匹配规则] 匹配模式: -name : 按 ...

  8. Spark基础:(一)初识Spark

    1.Spark中的Python和Scala的Shell (1): Python的Spark Shell 也就是我们常说的PySpark Shell进入我们的Spark目录中然后输入 bin/pyspa ...

  9. [php安全]原生类的利用

    php原生类的利用 查看原生类中具有魔法函数的类 $classes = get_declared_classes(); foreach ($classes as $class) { $methods ...

  10. JavaIO——File类

    1.File文件类 File类(描述具体文件或文件夹的类):是唯一一个与文件本身操作有关的程序类,可完成文件的创建.删除.取得文件信息等操作.但不能对文件的内容进行修改. (1)File类的基本使用 ...