-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path383.cpp
63 lines (59 loc) · 1.47 KB
/
383.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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <gtest/gtest.h>
#include <string>
#include <sys/types.h>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine)
{
unordered_map<char, int> mapm;
unordered_map<char, int> mapr;
for (const auto &c : magazine) {
mapm[c]++;
}
for (const auto &c : ransomNote) {
mapr[c]++;
}
for (const auto &p : mapr) {
if (mapm[p.first] < p.second) {
return false;
}
}
return true;
}
bool canConstruct2(string ransomNote, string magazine)
{
vector<int> map(26, 0);
for (const auto &c : magazine) {
map[c - 'a']++;
}
for (const auto &c : ransomNote) {
if (map[c - 'a'] == 0) {
return false;
}
map[c - 'a']--;
}
return true;
}
};
class Testing : public testing::Test {
public:
Solution sol;
};
TEST(Testing, CanConstruct)
{
Solution sol;
EXPECT_TRUE(sol.canConstruct("a", "a"));
EXPECT_FALSE(sol.canConstruct("a", "b"));
EXPECT_TRUE(sol.canConstruct("aa", "aab"));
EXPECT_FALSE(sol.canConstruct("aa", "ab"));
}
TEST(Testing, CanConstruct2)
{
Solution sol;
EXPECT_TRUE(sol.canConstruct2("a", "a"));
EXPECT_FALSE(sol.canConstruct2("a", "b"));
EXPECT_TRUE(sol.canConstruct2("aa", "aab"));
EXPECT_FALSE(sol.canConstruct2("aa", "ab"));
}