-
Notifications
You must be signed in to change notification settings - Fork 0
/
ransom_note.cpp
50 lines (48 loc) · 1.24 KB
/
ransom_note.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* =====================================================================================
*
* Filename: ransom_note.cpp
*
* Description: 383. Ransom Note https://leetcode.com/problems/ransom-note/
*
* Version: 1.0
* Created: 02/20/2022 12:10:24
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <array>
#include <cstdio>
#include <string>
class Solution {
public:
bool canConstruct(const std::string& ransom, const std::string& magazine) {
std::array<int, 26> counts = {0};
for (const char c : magazine) {
counts[c - 'a'] += 1;
}
for (const char c : ransom) {
counts[c - 'a'] -= 1;
if (counts[c - 'a'] < 0) {
return false;
}
}
return true;
}
};
int main(int argc, char* argv[]) {
std::string ransom = "aa";
std::string magazine = "aab";
if (argc > 2) {
ransom = argv[1];
magazine = argv[2];
}
const bool succ = Solution().canConstruct(ransom, magazine);
printf("ransom = %s, magazine = %s\n", ransom.c_str(), magazine.c_str());
printf("%s\n", succ ? "true" : "false");
return 0;
}