forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
23 lines (23 loc) · 890 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool validTicTacToe(vector<string>& board) {
auto count = [&](char x) {
int ans = 0;
for (auto& row : board)
for (auto& c : row) ans += c == x;
return ans;
};
auto win = [&](char x) {
for (int i = 0; i < 3; ++i) {
if (board[i][0] == x && board[i][1] == x && board[i][2] == x) return true;
if (board[0][i] == x && board[1][i] == x && board[2][i] == x) return true;
}
if (board[0][0] == x && board[1][1] == x && board[2][2] == x) return true;
return board[0][2] == x && board[1][1] == x && board[2][0] == x;
};
int x = count('X'), o = count('O');
if (x != o && x - 1 != o) return false;
if (win('X') && x - 1 != o) return false;
return !(win('O') && x != o);
}
};