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, ...
随机推荐
- 去掉input框点击时的默认颜色
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 < ...
- 一键删除.svn文件bat脚本
用过SVN或CVS版本控制工具的朋友,在享受着它们给我们带来的方便的同时,也许也在为这么一件事情苦恼: 如果某个目录在SVN或CVS版本控制工具的控制之下时.该目录下以及该子孙目录下都会有一个.svn ...
- 读取EXCEL数据到内存DataTable
protected void Page_Load(object sender, EventArgs e) { string filepath = Server.MapPath("~/file ...
- 转:WCF、WebAPI、WCFREST、WebService之间的区别
WCF.WebAPI.WCFREST.WebService之间的区别 注明:转载 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API ...
- 2012 Multi-University #8
DP+单调队列优化 E One hundred layer 题意:n*m的矩形,从第一层x位置往下走,每一层都可以往左或往右移动最多k步再往下走,问走到n层时所走路径的最大值. 分析:定义,,注意到m ...
- Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- jQuery学习之:Validation表单验证插件
http://polaris.blog.51cto.com/1146394/258781/ 最近由于公司决定使用AJAX + Struts2来重构项目,让我仔细研究一下这两个,然后集中给同事讲讲,让每 ...
- compass General 常用api学习[Sass和compass学习笔记]
compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...
- SQL中对于两个不同的表中的属性取差集except运算
SQL中对两个集合取差集运算,使用except关键字,语法格式如下: SELECT column_name(s) FROM table_name1 EXCEPT SELECT column_name( ...
- ubuntu安装配置elasticSearch(vagrant)
安装jdk sudo apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java s ...