leetcode 383. 赎金信
小于 1 分钟
给你两个字符串:ransomNote
和 magazine
,判断 ransomNote
能不能由 magazine
里面的字符构成。
如果可以,返回 true
;否则返回 false
。
magazine
中的每个字符只能在 ransomNote
中使用一次。
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int hash[26];
for(char c : ransomNote) {
hash[c - 'a']++;
}
for(char c : magazine) {
hash[c - 'a']--;
}
for(int n : hash) {
if(n > 0) {
return false;
}
}
return true;
}
};