1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: bool canConstruct(string ransomNote, string magazine) { int hash[26] = {0};
if(magazine.size() < ransomNote.size()) return false;
for(int i = 0; i < magazine.size(); i++) { hash[magazine[i] - 'a']++; }
for(int i = 0; i < ransomNote.size(); i++) { hash[ransomNote[i] - 'a']--; if(hash[ransomNote[i] - 'a'] < 0) return false; }
return true; } };
|