【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; 完
随机推荐
- python与各数据库的交互
from redis import StrictRedis from pymongo import MongoClient import pymysql #redis客户端 redis_cli = S ...
- 花样流水灯的verilog实现
LED(Light emitting diode)发光二极管将电能转化为可见光,正向电压导通,反向电压截止.对于该板子,二极管用低电压导通,其实验原理图为: 所谓流水灯,即让LED像水一样的点亮,从左 ...
- AutoCAD .NET二次开发(一)
其他话不多说,直接进入主题,既然是二次开发,当然是用CAD平台已经封装好了很多类,我们需要熟悉和使用它们.常用的AutoCAD .NET API的四个主要DLL文件是: 名称 作用 备注 AcDbMg ...
- java连数据库和数据库连接池踩坑日记(二)-------数据库连接池c3p0
关于数据库连接池,我觉得有些沮丧,因为最后被毙掉了说不用考虑多线程的问题…… 数据库连接池的推荐:https://www.cnblogs.com/nuccch/p/8120349.html 我最终选择 ...
- Tkinter 之事件绑定
import tkinter as tk window = tk.Tk() # 设置窗口大小 winWidth = 600 winHeight = 400 # 获取屏幕分辨率 screenWidth ...
- [代码审计]PHP_Bugs题目总结(2)
写的有点多了,上一篇放在一起显得有点臃肿,就再起一篇吧~ 迷路的老铁点这里:[代码审计]PHP_Bugs题目总结(1) 0x14 intval函数四舍五入 <?php if($_GET[id]) ...
- tecplot三维模型绘制二维切片流线
原视频下载地址链接: https://pan.baidu.com/s/1csugHK 密码: xrni
- js怎么模拟点击网页元素
在测试页面中,引入jquery源文件,并添加一个div标签,一个a标签,为了演示效果a标签暂时不添加地址 通过jquery为div标签绑定一个点击事件,这个事件是被动执行的.意思是要点击才会触发的 在 ...
- [English]常用中英文对照表
Always have been 一直如此 accordingly:相应地 assumption:假定 brace:大括号 branket:中括号 comma:逗号MISC:Miscellaneous ...
- 禁止select标签选择,禁止select组件change值
大家知道, 对于HTML控件select, 是没有readOnly属性的,所以设置它并不起作用,如: 如果用disabled的话,提交时又取不到值. 那有什么方法可以实现?可以有一个折中的方法,如下: ...