给你两个字符串:
ransomNote
和magazine
,判断ransomNote
能不能由magazine
里面的字符构成。如果可以,返回
true
;否则返回false
。
magazine
中的每个字符只能在ransomNote
中使用一次。示例 1:
输入:ransomNote = "a", magazine = "b" 输出:false示例 2:
输入:ransomNote = "aa", magazine = "ab" 输出:false示例 3:
输入:ransomNote = "aa", magazine = "aab" 输出:true
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int sum = ransomNote.size();
int map[27];
for(int i = 0; i < sum ;i++)
{
map[ransomNote[i] - 'a'] ++;
}
for(int i = 0; i < magazine.size(); i++)
{
map[magazine[i] - 'a']--;
}
for(int i = 0; i < 26; i++)
{
if(map[i] > 0)
return false;
}
return true;
}
};