#1275

Find Winner on a Tic Tac Toe Game

pupil · 435 · lc easy +25 · verified · 54.5% accepted · 1,625 likes · top 47%

Description

Players A and B alternate on a 3 x 3 tic-tac-toe grid. Player A goes first, placing 'X'; player B places 'O'. The game ends when one player fills a complete row, column, or diagonal, or when all nine squares are filled.

You are given moves where moves[i] = [rowi, coli] represents the ith move. Determine the outcome:

- Return "A" if player A wins.
- Return "B" if player B wins.
- Return "Draw" if the board is full with no winner.
- Return "Pending" if the game is not yet over.

Assume all moves are valid and the board starts empty.

Example 1:

Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: "A"
Explanation: A wins, they always play first.

Example 2:

Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
Output: "B"
Explanation: B wins.

Example 3:

Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
Output: "Draw"
Explanation: The game ends in a draw since there are no moves to make.

Code

1
2
3