forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-wording-game.cpp
41 lines (39 loc) · 1.31 KB
/
the-wording-game.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
// Time: O(m + n), m = sum(len(w) for w in a), n = sum(len(w) for w in b)
// Space: O(1)
// game, greedy
class Solution {
public:
bool canAliceWin(vector<string>& a, vector<string>& b) {
const auto& is_closely_greater = [](const auto& a, const auto& b) {
return a[0] - b[0] <= 1 && a > b;
};
bool result = true;
int i = 0, j = -1;
unordered_set<char> alphabet1;
for (const auto& w : a) {
alphabet1.emplace(w[0]);
}
unordered_set<char> alphabet2;
for (const auto& w : b) {
alphabet2.emplace(w[0]);
}
const int turn = size(alphabet1) + size(alphabet2);
for (int _ = 0; _ < turn; ++_) { // each player takes turns using a word with a different first letter than the last word he played
for (++j; j < size(b); ++j) {
if (is_closely_greater(b[j], a[i])) {
break;
}
}
if (j == size(b)) {
break;
}
while (j + 1 < size(b) && b[j + 1][0] == b[j][0]) { // play the lexicographically greatest word with the same first letter
++j;
}
swap(a, b);
swap(i, j);
result = !result;
}
return result;
}
};