Find Winner on a Tic Tac Toe Game LeetCode solution in Python

1
class Solution:
2
def tictactoe(self, moves: List[List[int]]) -> str:
3
wins = [
4
[(0, 0), (0, 1), (0, 2)],
5
[(1, 0), (1, 1), (1, 2)],
6
[(2, 0), (2, 1), (2, 2)],
7
[(0, 0), (1, 0), (2, 0)],
8
[(0, 1), (1, 1), (2, 1)],
9
[(0, 2), (1, 2), (2, 2)],
10
[(0, 0), (1, 1), (2, 2)],
11
[(0, 2), (1, 1), (2, 0)],
12
]
13

14
def checkWin(S):
15
for win in wins:
16
flag = True
17
for pos in win:
18
if pos not in S:
19
flag = False
20
break
21
if flag:
22
return True
23
return False
24

25
A, B = set(), set()
26
for i, (x, y) in enumerate(moves):
27
if i % 2 == 0:
28
A.add((x, y))
29
else:
30
B.add((x, y))
31

32
if checkWin(A):
33
return "A"
34
elif checkWin(B):
35
return "B"
36

37
return "Draw" if len(moves) == 9 else "Pending"

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0