// // Created by Li Zhilong on 11/6/21. // //Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise. // //Each letter in magazine can only be used once in ransomNote. // //Example 1: //Input: ransomNote = "a", magazine = "b" //Output: false // //Example 2: //Input: ransomNote = "aa", magazine = "ab" //Output: false // //Example 3: //Input: ransomNote = "aa", magazine = "aab" //Output: true #include #include #include #include using namespace std; class Solution { public: bool canConstruct(string ransomNote, string magazine) { std::unordered_map table; cout << "Mag size " << magazine.size() << endl; for (int i = 0; i < magazine.size(); i++) table.emplace(magazine[i], table[magazine[i]]++); // for (auto &x: table) // std::cout << x.first << ": " << x.second << std::endl; for (int i = 0; i < ransomNote.size(); i++) { table[ransomNote[i]] = --table[ransomNote[i]]; if (table[ransomNote[i]] < 0) return false; } return true; } bool canConstruct_vec(string ransomNote, string magazine) { std::vector table(26); for (int i=0; i