和242有效字母题很像,但只用看其中一个字符串,思路:先计算magazine中每个字母出现次数,然后在ransomnote中去除,当其小于0时,说明这个字符在magazine中没有出现或者已经使用过一次了。
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int record[26]={0};
if(ransomNote.size()>magazine.size())
{return false;}
for(int i=0;i<magazine.size();i++)
{
record[magazine[i]-'a']++;
}
for(int j=0;j<ransomNote.size();j++)
{
record[ransomNote[j]-'a']--;
if(record[ransomNote[j]-'a']<0)
{return false;}
}
return true;
}
};