Find Winner on a Tic Tac Toe Game Problem
Description
LeetCode Problem 1275.
Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:
- Players take turns placing characters into empty squares
' '. - The first player
Aalways places'X'characters, while the second playerBalways places'O'characters. 'X'and'O'characters are always placed into empty squares, never on filled ones.- The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
- The game also ends if all squares are non-empty.
- No more moves can be played if the game is over.
Given a 2D integer array moves where moves[i] = [row_i, col_i] indicates that the i^th move will be played on grid[row_i][col_i]. return the winner of the game if it exists (A or B). In case the game ends in a draw return "Draw". If there are still movements to play return "Pending".
You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.
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.
Constraints:
1 <= moves.length <= 9moves[i].length == 20 <= row_i, col_i <= 2- There are no repeated elements on
moves. movesfollow the rules of tic tac toe.
Solution
Approach: Check Only the Last Player
The moves are guaranteed to follow the rules, so nobody plays after the game is won. If there is a winner, it can only be the player who made the last move: had the other player completed a line earlier, the game would have stopped then.
So we only look at the last player's moves: moves n - 1, n - 3, n - 5, ... For these moves we count how many fall in each of the 8 lines of the board: rows 0 to 2, columns 0 to 2, the main diagonal (i == j) and the anti-diagonal (i + j == 2). One center move counts for its row, its column and both diagonals.
- If some line reaches
3, the last player won:"A"if the last move has an even index (A moves first), otherwise"B". - Otherwise nobody won:
"Draw"if all9squares are filled, else"Pending".
Note that a win on the ninth move is a win, not a draw, which is why we check for a winner before looking at the number of moves.
Complexity: O(n) time for n <= 9 moves, and O(1) space.
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
n = len(moves)
# Only the player who moved last can have won. count[0..2] are the rows,
# count[3..5] the columns, count[6] and count[7] the two diagonals.
count = [0] * 8
for k in range(n - 1, -1, -2):
i, j = moves[k]
count[i] += 1
count[3 + j] += 1
if i == j:
count[6] += 1
if i + j == 2:
count[7] += 1
if 3 in count:
return "A" if (n - 1) % 2 == 0 else "B"
return "Draw" if n == 9 else "Pending"