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. asp.net文件夹上传下载组件

    ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...

  2. Vim颜色配置

    最近迷上了Vim 主要原因是可以装逼 不过话说它自带的配色里面也就只有一个evening能勉强满足我的审美 于是我花了大概几天的时间翻了些百度贴吧,或者自己手动改属性后面的配色来实验这个属性到底对应哪 ...

  3. 洛谷P3964松鼠聚会

    题目 题意:求最小的从某一个点到其余点的切比雪夫距离和. 将一个图中的\((x,y)\)坐标转到新坐标\((x+y,x-y)\)后,图中的曼哈顿距离就是新图中的切比雪夫距离, 证明:分类讨论, 1.\ ...

  4. 使用docker安装wazuh

    使用docker安装wazuh centos下安装wazuh 官方文档: https://documentation.wazuh.com/3.9/installation-guide/installi ...

  5. UDP 区别于 TCP 的特点

    TCP 我们了解得多了,所以今天我们站在 UDP 的角度,探讨一下 UDP 区别于 TCP 的特点. 1. 面向无连接 UDP 比 TCP 简单得多,不需要“三次握手”来建立连接,直接把内容发送出去. ...

  6. 说明os,sys模块有什么不同

    官方解释: os: This module provides a portable way of usingoperating system dependent functionality. 翻译:提 ...

  7. SpringMVC处理请求释放静态资源的三种方式

    方式一 在SpringMVC的配置文件中添加如下语句 <mvc:default-servlet-handler/> 说明:当SpringMVC前端控制器对静态资源进行拦截后,在通过处理器映 ...

  8. shell脚本编程基础介绍

    Linux系统——shell脚本编程基础介绍 1.什么是shell 它是一个命令解释器,在linux/unix操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种操作输出的结果 ...

  9. vxlan和macvlan操作

    vxlan: 192.168.1.112 ———— 192.168.1.108 —— 192.168.1.109192.168.1.112配置:docker run -d -p 8500:8500 - ...

  10. #C++初学记录#日常学习函数

    静态变量static,只进行一次初始化. #include<cstring> #include<iostream> using namespace std; int main( ...