【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 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的更多相关文章
- 【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】1160. Find Words That Can Be Formed by Characters 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...
- 【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 ...
- 【POJ】1160 Post Office
http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...
- 【HDOJ】1160 FatMouse's Speed
DP. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXNUM 1005 ...
- 【Leetcode_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【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 ...
- 【Leetcode_easy】1025. Divisor Game
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完
- 【Leetcode_easy】1029. Two City Scheduling
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完
随机推荐
- PHP 将某一字符串转化为变量
1. $test = 'test'; $var = 'test'; echo $$var 运行结果为test 2. $this->test = 'test'; $var = 'test'; e ...
- PHP的3种发送HTTP请求的方式
1.CURL方式发送数据及上传文件 <?php class IndexController extends ControllerBase { public function indexActio ...
- C静态库和动态库的制作
(一)静态库就是把一些*.o的文件集合起来:以*.a结尾打包:做成的lib库文件:专门放到lib目录下 静态库的制作: 1.制作*.a文件 把之前src/mymath.c 的源文件 编译成单独的o文件 ...
- cf 911F 树的直径+贪心
$des$ 给定一棵 n 个节点的树,你可以进行 n ? 1 次操作,每次操作步骤如下:选择 u,v 两个度数为 1 的节点.将 u,v 之间的距离加到 ans 上.将 u 从树上删除.求一个操作序列 ...
- python3之线程(一)
线程的概念 现在的操作系统几乎都支持运行多个任务,而在操作系统内部,一个任务往往代表的执行的某一个程序,也就是运行中的程序,运行的程序是一个动态的概念,也就是所说的进程,而在进程内部,往往有许多顺序执 ...
- postman_
form-data 相当于Content-Type:multipart/form-data;它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件. x-www- ...
- postgresql【二】postgresql强制删除数据库
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='db_name' AND ...
- MYSQL | ERROR 1305(42000) SAVEPOINT *** DOES NOT EXIST
autocommit模式:在开启情况下,对于每条statement来说,都会自动形成一个commit,也就是会即时对开始和结束一个事务.所以,当出现rollback to savepoint出现这个错 ...
- Unity火爆插件Behavior Designer行为树插件学习
如果要让游戏里的角色或者NPC能执行预设的AI逻辑,最简单的用IF..ELSE...神器既可以实现, 但是再复杂的一般用经典的状态机来切换状态,但是写起来比较麻烦.相对的,行为树(Behavior T ...
- Zygote启动及其作用
目录 1.Zygote简介 2.Zygote进程如何启动 2.1 init.zygote64_32.rc文件 2.2 查看ps信息 2.3 启动 3.Zygote作用 3.1 启动system_ser ...