problem

1160. Find Words That Can Be Formed by Characters

solution

class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
int res = ;
unordered_map<char, int> charmap;
for(auto ch:chars) charmap[ch]++;
for(auto word:words)
{
unordered_map<char, int> tmp = charmap;
bool match = true;
for(auto ch:word)
{
if(tmp[ch]>) tmp[ch]--;//err...why count dont work...
else { match = false; break; }
}
if(match) res += word.size();
}
return res;
}/*
int countCharacters(vector<string>& words, string chars) {
int res = 0;
vector<int> charcnt(26);
for(auto ch:chars) charcnt[ch-'a']++;
for(auto word:words)
{
vector<int> tmp = charcnt;
bool match = true;
for(auto ch:word)
{
if(tmp[ch-'a']>0) tmp[ch-'a']--;//pay attention to index.
else { match = false; break; }
}
if(match) res += word.size();
}
return res;
}
*/
};

参考

1. Leetcode_easy_1160. Find Words That Can Be Formed by Characters;

【Leetcode_easy】1160. Find Words That Can Be Formed by Characters的更多相关文章

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

  2. 【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...

  3. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  4. 【POJ】1160 Post Office

    http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...

  5. 【HDOJ】1160 FatMouse's Speed

    DP. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXNUM 1005 ...

  6. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  7. 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers

    problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...

  8. 【Leetcode_easy】1025. Divisor Game

    problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完

  9. 【Leetcode_easy】1029. Two City Scheduling

    problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完

随机推荐

  1. go mod

    https://blog.csdn.net/zzhongcy/article/details/97243826 来自为知笔记(Wiz)

  2. Codeforces Global Round 6[A,B,C]

    题意:给一个字符串,对它重新排列使得它是60的倍数. 模拟一下,需要能整除60  字符串中需要 能整除2 3 10,所以需要字符串各位数之和能整除3 并且有 一个偶数和一个0  或者两个0也行[没考虑 ...

  3. img src防缓存

    //加时间戳防缓存 var imgurl = "/pcms/headImg/${sessionScope.accountInfo.accountId}_cut.jpg?time=" ...

  4. BurpSuite Intruder模块匹配返回包内容

    很多时候burpsuite intruder爆破我们是看返回包的长度,那么如何根据返回包的内容来做筛选呢? 这里我用的本地某cms环境做个演示 Intruder模块怎么用的不用介绍了吧 直接进入正题 ...

  5. [golang]golang 汇编

    https://lrita.github.io/2017/12/12/golang-asm/#why 在某些场景下,我们需要进行一些特殊优化,因此我们可能需要用到golang汇编,golang汇编源于 ...

  6. Samba文件共享服务设置

    SMB的主程序 smbd:SMB-TCP139,CIFS-TCP445 nmbd:NetBios-UDP137,138 SMB主程序对应的两个服务 /etc/init.d/smb /etc/init. ...

  7. java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?

    一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...

  8. Perl看完这个,再不敢说自己会玩贪吃蛇

    某天闲逛时看见一副动图: 真的是非常贪吃,各种拐弯各种吃,感觉十分有趣. 用Perl来实现自动吃满,蓄谋已久,之前的字符贪吃蛇.深度优先算法.A*算法,都是为此篇做铺垫. 那么,怎样让蛇不吃到自己呢? ...

  9. [spring-boot] 健康状况监控

    pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  10. postgresql【二】postgresql强制删除数据库

    SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='db_name' AND ...