1
class Solution {
2
public:
3
bool win(vector<string> board, char player, int size) {
4
int row_win = 0, col_win = 0, diagonal_win = 0, rev_diagonal_win = 0;
5

6
for (int i = 0; i < 3; i++) {
7
row_win = 0;
8
col_win = 0;
9

10
for (int j = 0; j < 3; j++) {
11
if (player == board[i][j]) {
12
row_win++;
13
if (row_win == 3) return 1;
14
}
15

16
if (player == board[j][i]) {
17
col_win++;
18
if (col_win == 3) return 1;
19
}
20

21
if (i == j && player == board[i][j]) {
22
diagonal_win++;
23
}
24

25
if (i + j == size - 1 && player == board[i][j]) {
26
rev_diagonal_win++;
27
}
28
}
29
}
30

31
if (diagonal_win == 3 || rev_diagonal_win == 3) return 1;
32

33
return 0;
34
}
35

36
bool validTicTacToe(vector<string> &board) {
37
int count_x = 0, count_o = 0, size = board.size();
38
for (auto i : board) {
39
for (auto j : i) {
40
if (j == 'X')
41
count_x++;
42
else if (j == 'O')
43
count_o++;
44
}
45
}
46
if (count_o > count_x || count_x - count_o >= 2) return false;
47
if (count_x >= 3 && count_x == count_o && win(board, 'X', size)) return false;
48
if (count_o >= 3 && count_x > count_o && win(board, 'O', size)) return false;
49

50
return true;
51
}
52
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0