一:题目 二:上码 1:第一种方法 class Solution { public:bool canConstruct(string ransomNote, string magazine) {unordered_map<char,int>m,m1;for(int j = 0; j < magazine.size(); j++) {m[magazine[j]]++;}for(int i = 0; i < ransomNote.size(); i++) {m1[ransomNote[i]]++;}int count = 0;for(int i = 0; i < ransomNote.size(); i++) {if(m.find(ransomNote[i]) != m.end() && m[ransomNote[i]] >= m1[ransomNote[i]]){ count++;}}if(count != ransomNote.size()){return false;}return true;} }; 2:第二种方法 class Solution { public:bool canConstruct(string ransomNote, string magazine) {int record[26] = {0};//记录magazine中字符出现的个数for(auto ch: magazine) {record[ch-'a']++;}for(auto ch: ransomNote) {record[ch-'a']--;//如果ransomNode中的字符在magazine中没有出现过,那么的话recode[i] 将小于0if(record[ch-'a'] < 0){return false;}}return true;} };