383. Ransom Note【easy】

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> m_rans; for (int i = ; i < ransomNote.length(); ++i) {
++m_rans[ransomNote[i]];
} for (int i = ; i < magazine.length(); ++i) {
if (m_rans.find(magazine[i]) != m_rans.end()) {
--m_rans[magazine[i]];
}
} for (map<char, int>::iterator it = m_rans.begin(); it != m_rans.end(); ++it) {
if (it->second > ) {
return false;
}
} return true;
}
};

解法二:

 public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[];
for (int i = ; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = ; i < ransomNote.length(); i++) {
if(--arr[ransomNote.charAt(i)-'a'] < ) {
return false;
}
}
return true;
}
}

参考@yidongwang 的代码。

解法三:

 class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> map();
for (int i = ; i < magazine.size(); ++i)
++map[magazine[i]];
for (int j = ; j < ransomNote.size(); ++j)
if (--map[ransomNote[j]] < )
return false;
return true;
}
};

参考@haruhiku 的代码

解法四:

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

参考@haruhiku 的代码

383. Ransom Note【easy】的更多相关文章

  1. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  2. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  3. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  4. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  7. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  8. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  9. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

随机推荐

  1. [BZOJ3786]星系探索(伪ETT)

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1638  Solved: 506[Submit][Status][Discuss ...

  2. POJ 3688 Cheat in the Game(博弈论)

    [题目链接] http://poj.org/problem?id=3688 [题目大意] 有俩人玩一个取石子的游戏,你是裁判. 游戏中有W块石头和N张卡片,卡片上分别写着数字Ai. 玩家随机抽走一张卡 ...

  3. [LOJ6436]神仙的游戏

    感觉border的性质还是挺神奇的 一个border的性质是$S$有长度为$len$的border当且仅当对$\forall i\equiv j\left(\bmod(n-len)\right)$有$ ...

  4. 编译boost到各个系统平台 mac,iOS,linux,android,wind

    编译boost到各个系统平台 mac,iOS,linux,android,wind git地址:https://github.com/czjone/boost git仓库:https://github ...

  5. linux下查看端口占用情况以及服务启动的目录

    1.先介绍几个命令: 1. lsof -i:80 查看80端口的占用情况 命令返回结果: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME ngin ...

  6. MetaWeblog是什么

    MetaWebBlog API(MWA)是一个Blog程序接口标准,允许外部程序来获取或者设置Blog的文字和熟悉.他建立在XMLRPC接口之上,并且已经有了很多的实现. 所以现在很多博客系统都支持这 ...

  7. Sublime text JsFormat插件的安装

    javascript格式化插件JsFormat 1.下载这插件包 https://github.com/jdc0589/JsFormat 2.点击菜单:Preferences->Browse P ...

  8. shell脚本编写注意事项

    shell中赋值变量时不能有空格 之前写python写习惯了 test = ‘free -m’ 在shell中不能有空格 test='free -m' 而且使用管道符之前要留空格 test='free ...

  9. 一天干掉一只Monkey计划(一)——基本光照模型及RT后处理 【转】

    http://www.cnblogs.com/Zephyroal/archive/2011/10/10/2206530.html 一天干掉一只Monkey计划(一)——基本光照模型及RT后处理 1, ...

  10. solr6.6 高级搜索Facet

    1.介绍 facet分面查询是solr中以导航为目的的查询,在用户查询的结果上根据分类增加了count信息,然后用户根据count信息做进一步实现渐进式精确搜索. 什么字段适合用facet呢?  fa ...