leetcode 383. Ransom Note

时间:2022-12-30 07:54:56


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的更多相关文章

  1. 14&period; leetcode 383&period; Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  2. Java &lbrack;Leetcode 383&rsqb;Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  3. LeetCode&colon; 383 Ransom Note&lpar;easy&rpar;

    题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...

  4. 383&period; Ransom Note【easy】

    383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...

  5. 【LeetCode】383&period; Ransom Note 解题报告&lpar;Java & Python&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  6. leetcode修炼之路——383&period; Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

  7. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 383&period; Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  8. 383&period; Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  9. &lbrack;LeetCode&rsqb; 383&period; Ransom Note&lowbar;Easy tag&colon; Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

随机推荐

  1. Android——黑名单

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. JPA 不在 persistence&period;xml 文件中配置每个Entity实体类的2种解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法.   这2种方式都可以实现不用persist ...

  3. R语言将5位数字日期转为正常日期

    R语言中用double表示日期,即从1970-1-1距离给定日期的天数,将5位数字日期转为正常日期格式的方法 as.Date(16543,origin='1970-1-1')

  4. LocalDB 的创建与迁移

    首先创建对应的对象 public class Movie { public int ID { get; set; } public string Title { get; set; } public ...

  5. poj1639 Picnic Planning 最小度数限制生成树

    题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...

  6. myeclipse8&period;6安装svn插件

    1.从官方网站下载site-1.6.16.zip,网址:subclipse.tigris.org: 2.将解压出来的features与plugins,复制到任意目录:Genuitec/MyEclips ...

  7. Android Paint画笔及Color &period;

    引自:http://blog.csdn.net/q445697127/article/details/7736926 Paint paint = new Paint(); // 设置paint为无锯齿 ...

  8. taro中子父传值

    其实网上很多方法,我这只是一个简单的demo,废话不多说直接上代码 import Taro, { Component } from '@tarojs/taro' import { View, Text ...

  9. &lbrack;docker&rsqb;docker4种网络最佳实战

    参考: http://hicu.be/docker-container-network-types docker默认3中网络类型 参考: https://docs.docker.com/engine/ ...

  10. 关于UC浏览器兼容scroll事件问题

    经过本人查阅无数资料,最终得出一个比较简单,具有一定兼容性的结果. $(window).scroll(function( ) { var scrollTop = document.documentEl ...