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. JavaScript基础——数组

    一 .数组的介绍 1.概念:数据的集合,任何数据都可以放在数组中 2.作用:可以同时操作多个数据 3.数组的创建: 字面量:var arr = [ ]; 构造函数:var arr = new Arra ...

  2. 3 Ways to Force Unmount in Linux Showing “device is busy”

    3 Ways to Force Unmount in Linux Showing “device is busy” Updated August 8, 2019By Bobbin ZachariahL ...

  3. 博客系统的使用(typecho、WordPress等等)

    一.下载,解压,安装 二.Apache配置虚拟主机,(host文件修改) 三.开启php.ini中pdo类型的扩展适配数据库 四.按照指示页面配置 五.操作控制面板和blog前台

  4. 那些被Oracle优化的程序员

    甲骨文(中国)是一家很有趣的公司,势头强劲,却被公认为养老公司(俗称西二旗养老院).风光无限,却也走上了裁员之路. 据甲骨文员工透漏,前一天晚上还在加班改bug,第二天就通知被裁了,甚至要求被裁员工在 ...

  5. P1902 刺杀大使

    题目描述 伊朗伊斯兰革命卫队(某恐怖组织)正在策划一起刺杀行动,他们的目标是沙特驻美大 使朱拜尔.他们来到了沙特驻美使馆,准备完成此次刺杀,要进入使馆首先必须通过使馆前 的防御迷阵. 迷阵由 n*m ...

  6. 转载 | Python AI 教学│k-means聚类算法及应用

    关注我们的公众号哦!获取更多精彩哦! 1.问题导入 假如有这样一种情况,在一天你想去某个城市旅游,这个城市里你想去的有70个地方,现在你只有每一个地方的地址,这个地址列表很长,有70个位置.事先肯定要 ...

  7. 洛谷P1325 雷达安装

    题目 考虑对于一个小岛,如果有雷达可以覆盖它,则这些雷达肯定在一个区间里,则原题内容则变为区间选点问题 #include <bits/stdc++.h> using namespace s ...

  8. 乌班图下fluent开启并行报错的解决方法

    参考链接: CFD-online原帖:http://www.cfd-online.com/Forums/fluent/149668-fluent-16-0-0-ubuntu-12-04-a.html ...

  9. DELPHI开发LINUX的动态库

    DELPHI开发LINUX的动态库 WINDOWS的动态库是.dll,这个大家都知道. LINUX也有动态库,扩展名是.so,现在DELPHI也能开发LINUX的动态库哦. DELPHI对LINUX的 ...

  10. 做动画animation--matplotlib--python2和3通用代码

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_42053726/artic ...