题目:

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) {
if (ransomNote == "")
return true;
for (auto c : ransomNote){
int position = magazine.find_first_of(c);
if (position != magazine.npos)
magazine.erase(position, );
else
return false;
}
return true;
}
};

别人的:

 class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int mp[] = {};
for(auto c : magazine) {
mp[c - 'a']++;
}
for(auto r : ransomNote) {
mp[r - 'a']--;
if(mp[r - 'a'] < ) return false;
}
return true;
}
};

LeetCode: 383 Ransom Note(easy)的更多相关文章

  1. leetcode 383. Ransom Note

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

  2. 14. leetcode 383. Ransom Note

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

  3. Java [Leetcode 383]Ransom Note

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

  4. 383. Ransom Note【easy】

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

  5. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

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

  6. leetcode修炼之路——383. Ransom Note

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

  7. [LeetCode&Python] Problem 383. Ransom Note

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

  8. 383. Ransom Note

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

  9. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

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

随机推荐

  1. 九度OJ 1007:奥运排序问题 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7344 解决:1568 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号从0到N- ...

  2. pgsql 数据类型

  3. python数据分析之:数据清理,转换,合并,重塑(二)

    一:移除重复数据 DataFrame经常出现重复行,就像下面的这样 In [7]: data=DataFrame({'k1':['one']*3+['two']*4,'k2':[1,1,2,3,3,4 ...

  4. http://blog.csdn.net/wh211212/article/details/53005321

    http://blog.csdn.net/wh211212/article/details/53005321

  5. 《CSS权威指南(第三版)》---第四章 值和单位

    本章主要讲解的是一些属性声明用的值: CSS中的值主要有数字,百分数,颜色, 1.颜色: rgb(100%,100%,100%)  OR  rgb(255,255,255) OR #FF0000 WE ...

  6. Contiki 2.7 Makefile 文件(四)

    3.第三部分 这里我们假设TARGET为native (1) OBJECTDIR = obj_$(TARGET) LOWERCASE = -abcdefghijklmnopqrstuvwxyz UPP ...

  7. ss命令能识别的TCP状态的关键字

    [TCP_ESTABLISHED] = "ESTAB",         [TCP_SYN_SENT] = "SYN-SENT",         [TCP_S ...

  8. centos 7 部署 mysql 报错记录

    1. Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY 这是由于yum安装了旧版本的GPG keys造成的,解决办法就是 引用  rpm --i ...

  9. 常用JS组件整理

    1.漂亮的弹出层----artDialog http://aui.github.io/artDialog/ 2.弹出层 ------layer http://sentsin.com/jquery/la ...

  10. html5--1.4元素的属性

    html5--1.4元素的属性 学习要点: 1.了解HTML元素属性2.学习两个属性:align和bgcolor 属性的作用就是就为元素提供更多的信息,大多数元素都可以拥有属性 属性的语法:<标 ...