leetcode383 赎金信
和242题一样的思路
一些同学可能想,用数组干啥,都用map完事了,其实在本题的情况下,使用map的空间消耗要比数组大一些的,因为map要维护红黑树或者哈希表,而且还要做哈希函数,是费时的!数据量大的话就能体现出来差别了。 所以数组更加简单直接有效!
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int count[26] = {0};
for(char c: magazine){
count[c - 'a'] ++;
}
for(char c: ransomNote){
count[c - 'a'] --;
}
for(int i = 0; i < 26; i++){
if(count[i] < 0) return false;
}
return true;
}
};