2025-08-02 14:06:00 +0000 UTC
Find Winner on a Tic Tac Toe Game
Categories:
Links
Code
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
cols = [[0] * 3 for _ in range(2)]
rows = [[0] * 3 for _ in range(2)]
diags = [[0] * 2 for _ in range(2)]
players = ["A", "B"]
for i, (row, col) in enumerate(moves):
if i % 2 == 0:
player = 0
else:
player = 1
rows[player][row] += 1
cols[player][col] += 1
if row == col:
diags[player][0] += 1
if row == 2 - col:
diags[player][1] += 1
for player in range(2):
for win in (cols, rows, diags):
if 3 in win[player]:
return players[player]
if len(moves) == 9:
return "Draw"
return "Pending"