-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidTic-Tac-ToeState.cpp
More file actions
32 lines (30 loc) · 1.15 KB
/
validTic-Tac-ToeState.cpp
File metadata and controls
32 lines (30 loc) · 1.15 KB
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
// Source: https://leetcode.com/problems/valid-tic-tac-toe-state/
// Author: Miao Zhang
// Date: 2021-03-11
class Solution {
public:
bool validTicTacToe(vector<string>& board) {
int x_count = 0;
int o_count = 0;
for (string& row: board) {
for (char c: row) {
if (c == 'X') x_count++;
if (c == 'O') o_count++;
}
}
if (o_count != x_count - 1 && o_count != x_count) return false;
if (win(board, 'X') && o_count != x_count - 1) return false;
if (win(board, 'O') && x_count != o_count) return false;
return true;
}
private:
bool win(vector<string>& board, char player) {
for (int i = 0; i < 3; i++) {
if (player == board[i][0] && player == board[i][1] && player == board[i][2]) return true;
if (player == board[0][i] && player == board[1][i] && player == board[2][i]) return true;
}
if (player == board[0][0] && player == board[1][1] && player == board[2][2]) return true;
if (player == board[0][2] && player == board[1][1] && player == board[2][0]) return true;
return false;
}
};