题目地址: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. 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++代码如下:

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++)的更多相关文章

  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. 【GS文献】基因组选择技术在农业动物育种中的应用

    中国农业大学等多家单位2017年合作发表在<遗传>杂志上的综述,笔记之. 作者中还有李宁院士,不胜唏嘘. 1.概述 GS的两大难题:基因组分型的成本,基因组育种值(genomic esti ...

  2. Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error

    Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error   在安装目录下执行 vim obj/Markfile 把 ...

  3. linux下vim的安装与配置(centos)

    1.vim的安装 #yum search vim   //查看vim相关软件信息 #yum install -y vim*  //在线安装vim 2.vim的配置 (1)~/.viminfo 在vim ...

  4. js判断undefined nan等

    1,js判断undefined 主要用typeof(),typeof的返回值有:undefined,object,boolean,number,string,symbol,function等, if( ...

  5. 用原生CSS编写动态字体

    HTML部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  6. c#表中信息点击跳转

    OnRowCommand="gridInfoData_RowCommand" <Columns> <asp:ButtonField HeaderText=&quo ...

  7. jsp的动态包含和静态包含

    jsp的动态包含和静态包含 例如:提取一个公共的页面(top.jsp)到/WEB-INF/jsp/common/目录下 动态包含: 被包含的页面也会独立编译,生成字节码文件,一般包含页面信息频繁变化的 ...

  8. 日常Java 2021/11/2

    Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型.将序列化对象写入文件之后,可以从文件中读取出来 ...

  9. WebService学习总览

    [1]WebService简介 https://blog.csdn.net/xtayfjpk/article/details/12256663 [2]CXF中Web服务请求处理流程 https://b ...

  10. Linux磁盘分区(一)之fdisk命令

    Linux磁盘分区(一)之fdisk命令转自:https://www.cnblogs.com/machangwei-8/p/10353683.html 一.fdisk 的介绍fdsik 能划分磁盘成为 ...