-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathransom_note.cpp
More file actions
60 lines (50 loc) · 1.2 KB
/
Copy pathransom_note.cpp
File metadata and controls
60 lines (50 loc) · 1.2 KB
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
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
if(ransomNote.empty())
{
return true;
}
if(magazine.empty())
{
return false;
}
int note_character[26] = {0};
int magazine_character[26] = {0};
for(auto c : ransomNote)
{
note_character[c-'a']++;
}
for(auto c : magazine)
{
magazine_character[c-'a']++;
}
bool flag = true;
for(auto i = 0; i < 26; i++)
{
if(note_character[i] > magazine_character[i])
{
flag = false;
break;
}
}
return flag;
}
};
int main(void)
{
Solution solution;
string note1 = "a";
string magazine1 = "b";
string note2 = "aa";
string magazine2 = "ab";
string note3 = "aa";
string magazine3 = "aba";
cout << solution.canConstruct(note1,magazine1) << endl;
cout << solution.canConstruct(note2,magazine2) << endl;
cout << solution.canConstruct(note3,magazine3) << endl;
return 0;
}