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, ...
随机推荐
-
Android——黑名单
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
-
JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法
在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法. 这2种方式都可以实现不用persist ...
-
R语言将5位数字日期转为正常日期
R语言中用double表示日期,即从1970-1-1距离给定日期的天数,将5位数字日期转为正常日期格式的方法 as.Date(16543,origin='1970-1-1')
-
LocalDB 的创建与迁移
首先创建对应的对象 public class Movie { public int ID { get; set; } public string Title { get; set; } public ...
-
poj1639 Picnic Planning 最小度数限制生成树
题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...
-
myeclipse8.6安装svn插件
1.从官方网站下载site-1.6.16.zip,网址:subclipse.tigris.org: 2.将解压出来的features与plugins,复制到任意目录:Genuitec/MyEclips ...
-
Android Paint画笔及Color .
引自:http://blog.csdn.net/q445697127/article/details/7736926 Paint paint = new Paint(); // 设置paint为无锯齿 ...
-
taro中子父传值
其实网上很多方法,我这只是一个简单的demo,废话不多说直接上代码 import Taro, { Component } from '@tarojs/taro' import { View, Text ...
-
[docker]docker4种网络最佳实战
参考: http://hicu.be/docker-container-network-types docker默认3中网络类型 参考: https://docs.docker.com/engine/ ...
-
关于UC浏览器兼容scroll事件问题
经过本人查阅无数资料,最终得出一个比较简单,具有一定兼容性的结果. $(window).scroll(function( ) { var scrollTop = document.documentEl ...