-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1662_arrayStringsAreEqual.cc
84 lines (77 loc) · 2.07 KB
/
Problem_1662_arrayStringsAreEqual.cc
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <algorithm>
#include <iostream> // for_each
#include <numeric> // accumulate
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
bool arrayStringsAreEqual1(vector<string> &word1, vector<string> &word2)
{
auto p1 = std::accumulate(word1.begin(), word1.end(), string(""), [](string &s1, string &s2) { return s1 + s2; });
auto p2 = std::accumulate(word2.begin(), word2.end(), string(""), [](string &s1, string &s2) { return s1 + s2; });
return p1 == p2;
}
bool arrayStringsAreEqual2(vector<string> &word1, vector<string> &word2)
{
string p3;
string p4;
std::for_each(word1.begin(), word1.end(), [&](const string &s) { p3 += s; });
std::for_each(word2.begin(), word2.end(), [&](const string &s) { p4 += s; });
return p3 == p4;
}
bool arrayStringsAreEqual3(vector<string> &word1, vector<string> &word2)
{
int i = 0;
int j = 0;
int p = 0;
int q = 0;
int n = word1.size();
int m = word2.size();
while (i < n && j < m)
{
if (word1[i][p++] != word2[j][q++])
{
return false;
}
if (p == word1[i].length())
{
i++;
p = 0;
}
if (q == word2[j].length())
{
j++;
q = 0;
}
}
return i == n && j == m;
}
};
void testArrayStringsAreEqual()
{
Solution s;
vector<string> n1 = {"ab", "c"};
vector<string> n2 = {"a", "bc"};
vector<string> n3 = {"a", "cb"};
vector<string> n4 = {"ab", "c"};
vector<string> n5 = {"abc", "d", "defg"};
vector<string> n6 = {"abcddefg"};
EXPECT_TRUE(s.arrayStringsAreEqual1(n1, n2));
EXPECT_FALSE(s.arrayStringsAreEqual1(n3, n4));
EXPECT_TRUE(s.arrayStringsAreEqual1(n5, n6));
EXPECT_TRUE(s.arrayStringsAreEqual2(n1, n2));
EXPECT_FALSE(s.arrayStringsAreEqual2(n3, n4));
EXPECT_TRUE(s.arrayStringsAreEqual2(n5, n6));
EXPECT_TRUE(s.arrayStringsAreEqual3(n1, n2));
EXPECT_FALSE(s.arrayStringsAreEqual3(n3, n4));
EXPECT_TRUE(s.arrayStringsAreEqual3(n5, n6));
EXPECT_SUMMARY;
}
int main()
{
testArrayStringsAreEqual();
return 0;
}