Created
July 18, 2022 13:27
-
-
Save icameling/1b17cffc3b897e801b7e3d2ec78a5c15 to your computer and use it in GitHub Desktop.
#哈希表 #赎金信
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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