-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_2451_oddString.cc
More file actions
53 lines (48 loc) · 938 Bytes
/
Copy pathProblem_2451_oddString.cc
File metadata and controls
53 lines (48 loc) · 938 Bytes
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
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<int> getDiff(string &s)
{
int n = s.length();
vector<int> diff(n - 1);
for (int i = 0; i < n - 1; i++)
{
diff[i] = s[i + 1] - s[i];
}
return diff;
}
string oddString(vector<string> &words)
{
auto d0 = getDiff(words[0]);
auto d1 = getDiff(words[1]);
if (d0 == d1)
{
for (int i = 2; i < words.size(); i++)
{
if (d0 != getDiff(words[i]))
{
return words[i];
}
}
}
return d0 == getDiff(words[2]) ? words[1] : words[0];
}
};
void testOddString()
{
Solution s;
vector<string> w1 = {"adc", "wzy", "abc"};
vector<string> w2 = {"aaa", "bob", "ccc", "ddd"};
EXPECT_TRUE("abc" == s.oddString(w1));
EXPECT_TRUE("bob" == s.oddString(w2));
EXPECT_SUMMARY;
}
int main()
{
testOddString();
return 0;
}