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

题目大意:给两个string,判断第一个string能否由第二个string里面所含有的字母组成,第二个string里面的所有字母只能使用一次~

分析:建立一个hash数组,对第二个string遍历并记录每个字符出现的次数,然后遍历第一个string,如果有出现hash里面不存在的字符,那么return false~

class Solution {
public:bool canConstruct(string ransomNote, string magazine) {vector<int> hash(256);for(int i = 0; i < magazine.size(); i++)hash[magazine[i]]++;for(int i = 0; i < ransomNote.size(); i++)if(hash[ransomNote[i]])hash[ransomNote[i]]--;elsereturn false;return true;}
};

LeetCode383. Ransom Note相关推荐

  1. LeetCode:383. Ransom Note

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

  2. [LeetCode] NO.383 Ransom Note

    [题目] Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all ...

  3. Leetcode 383 Ransom Note

    lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...

  4. LeetCode之Ransom Note

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

  5. LeetCode 383. Ransom Note

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

  6. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  7. Ransom Note

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

  8. 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续

    383  easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...

  9. LeetCode Ransom Note(字符串)

    题意:给出两个字符串s1,s2 ,看s1是否可以由s2中的字符组成 具体代码如下: public class Solution {public boolean canConstruct(String ...

最新文章

  1. C# string.Format谨慎使用
  2. HDU 3555 Bomb
  3. centreon问题总结
  4. sublime text常用快捷键及多行光标批量操作教程
  5. python哈夫曼编码注意_Python 算法(2) 哈夫曼编码 Huffman Encoding
  6. throws Exception的意思
  7. 关于python django.url问题 求助
  8. CUDA2.1-原理之索引与warp
  9. SQL Server 日期函数:EOMonth、DateFormat、Format、DatePart、DateName
  10. isinstance / issubclass / type, 方法和函数, 反射
  11. Javascript 加载详解
  12. 完整卸载SQL Server2008
  13. 几种常用的网络传输协议
  14. C++学习记录vs2013 植物大战僵尸mfc辅助大体框架编写
  15. “全球化”是一个漫长过程,海尔智家用了20年
  16. php中求解二元一次方程,基于Algebra.js解二元一次方程的实例教程
  17. LS1046修改寄存器翻转SATA引脚的N/P极性
  18. DBlink 创建 删除 脚本
  19. NASA顶级程序员编程十大原则
  20. python将秒换成天时分秒

热门文章

  1. xulrunner弹出窗口不显示解决方法
  2. My first project
  3. 用户自定义函数代替游标进行循环拼接
  4. HA3、iGraph和DII服务框架统一之战
  5. Linux内存初始化(四) 创建系统内存地址映射
  6. bootstrap ie兼容
  7. centos vim编辑器 第八节课
  8. 通过jQuery.support看javascript中的兼容性问题
  9. thinking in java(第七章)
  10. Recompile/upgrade nginx binary with no down-time