给定一个任意的表示勒索信内容的字符串,和另一个字符串表示杂志的内容,写一个方法判断能否通过剪下杂志中的内容来构造出这封勒索信,若可以,返回 true;否则返回 false。
杂志字符串中的每一个字符仅能在勒索信中使用一次。
样例 1
输入 : ransomNote = "aa", magazine = "aab"
输出 : true
解析 : 勒索信的内容可以有杂志内容剪辑而来
样例 2
输入 : ransomNote = "aaa", magazine = "aab"
输出 : false
解析 : 勒索信的内容无法从杂志内容中剪辑而来
class Solution {
public:
/**
* @param ransomNote: a string
* @param magazine: a string
* @return: whether the ransom note can be constructed from the magazines
*/
bool canConstruct(string &ransomNote, string &magazine) {
// Write your code here
map<char,int> ran;
for (auto i : magazine) {
/* code */
ran[i]++;
}
for (auto i : ransomNote) {
/* code */
if(ran[i]==0) return false;
else ran[i]--;
}
return true;
}
};