Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 18, 2022 13:27
Show Gist options
  • Save icameling/1b17cffc3b897e801b7e3d2ec78a5c15 to your computer and use it in GitHub Desktop.
Save icameling/1b17cffc3b897e801b7e3d2ec78a5c15 to your computer and use it in GitHub Desktop.
#哈希表 #赎金信
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
if (ransomNote.size() > magazine.size()) return false;
int table[26] = {0};
for (int i = 0; i < magazine.size(); ++i) {
table[magazine[i] - 'a']++;
}
for (int i = 0; i < ransomNote.size(); ++i) {
table[ransomNote[i] - 'a']--;
}
for (int i = 0; i < 26; ++i) {
if (table[i] < 0)
return false;
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment