1
class Solution:
2
def validTicTacToe(self, board: List[str]) -> bool:
3
# The two criteria for a valid board are:
4
# 1) num of Xs - num of Os is 0 or 1
5
# 2) X is not a winner if the # of moves is even, and
6
# O is not a winner if the # of moves is odd.
7

8
d = {"X": 1, "O": -1, " ": 0} # transform the 1x3 str array to a 1x9 int array
9
s = [
10
d[ch] for ch in "".join(board)
11
] # Ex: ["XOX"," X "," "] --> [1,-1,1,0,1,0,0,0,0]
12
sm = sum(s)
13

14
if sm >> 1:
15
return False # <-- criterion 1
16

17
n = -3 if sm == 1 else 3 # <-- criterion 2.
18
if n in {
19
s[0] + s[1] + s[2],
20
s[3] + s[4] + s[5],
21
s[6] + s[7] + s[8],
22
s[0] + s[3] + s[6],
23
s[1] + s[4] + s[7],
24
s[2] + s[5] + s[8], # the elements of the set are
25
s[0] + s[4] + s[8],
26
s[2] + s[4] + s[6],
27
}:
28
return False # the rows, cols, and diags
29

30
return True # <-- both criteria are true

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0