leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char,int> mpr,mpm;
map<char,int>::iterator itr,itm;
int i ,j;
for(i=; i < ransomNote.length();++i)
{
mpr[ransomNote[i]]++;
}
for(j = ; j<magazine.length();++j)
{
mpm[magazine[j]]++;
}
if(mpr.size() > mpm.size()) return false;
itr = mpr.begin();itm = mpm.begin();
while(itr != mpr.end() && itm != mpm.end())
{
char tmp = itr->first;
map<char,int>::iterator t;
if(mpm.find(tmp) == mpm.end())
return false;
if((int)(itr->second) > (int)(mpm.find(tmp)->second))
return false;
else
itr++;
}
return true;
}
};
leetcode 383. Ransom Note的更多相关文章
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
随机推荐
- AngularJS 事件
AngularJS 有自己的 HTML 事件指令. ng-click指令: ng-click 指令定义了 AngularJS 点击事件. <!DOCTYPE html> <html& ...
- 在Windows10 64位 Anaconda4 Python3.5下安装XGBoost
系统环境: Windows10 64bit Anaconda4 Python3.5.1 软件安装: Git for Windows MINGW 在安装的时候要改一个选择(Architecture选择x ...
- DB2 license过期的问题
今天启动DB2,无论如何都启动不了,报一个错误:“Windows 不能在 本地计算机 启动 DB2 - DB2COPY - DB2-0.有关更多信息,查阅系统事件日志.如果这是非 Microsoft ...
- Smooth Mouse
一款免费软件,使mac可以做到和 Windows 下的鼠标加速度的相同算法,实现同样 win 的鼠标操控感,也可以实现鼠标0加速度.
- 简述抽象和封装,对你学习Java有一些作用
作为一个Java 初学者,对Java的理解可能有些片面,甚至有些错误的理解,对于观看此处的您,希望您选择性观看!!! 天知道我为什么选择学习编程,我不爱编程,但是我既然学习了,还是会努力学习的,在此分 ...
- CodeForces 518B. Tanya and Postcard
B. Tanya and Postcard time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 2016 Multi-University Training Contest 1 F.PowMod
PowMod Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- 如何记录搜索引擎爬行记录php版
写博客也有一段时间了,为什么搜索引擎迟迟不收录你的页面呢?想知道每天都有哪些蜘蛛“拜访”你的网站吗?作为一名网站长,有必要知道每天都有哪些蜘蛛爬行过你的网站,以便于了解各搜索引擎蜘蛛爬行频率,对网站进 ...
- windows 安装MySql
转载:http://blog.csdn.net/longyuhome/article/details/7913375 Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系 ...
- 日志分析_使用shell完整日志分析案例
一.需求分析 1. 日志文件每天生成一份(需要将日志文件定时上传至hdfs) 2. 分析日志文件中包含的字段:访问IP,访问时间,访问URL,访问状态,访问流量 3. 现在有"昨日" ...