力扣383. 赎金信
思路
开始想的是用set查找删除,后来想到字母是可以穷举的,而且用set删除不如数组快,就采用数组了。
先使用数组存储后一个数组,然后针对前一个数组的每个元素进行删除。删到小于0就说明没有匹配元素就返回FALSE。
代码
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int notes[26]={0};
for(char letter:magazine){
notes[letter-'a']++;
//cout<<"letter is "<<letter<<endl;
}
for(char letter2:ransomNote){
notes[letter2-'a']--;
if(notes[letter2-'a']<0) return false;
}
return true;
}
};