Show problems as
Classic problem #1275EasyArrayHash TableMatrixSimulation

Find Winner on a Tic Tac Toe Game

Real-world version: Who Comes Out Ahead on the Three-by-Three
Tracked in this browser only
Read Find Winner on a Tic Tac Toe Game on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Two players take turns marking cells of a three-by-three board, the first player writing "A" and the second "B". The plays are given in order as moves, each [row, column], with the first entry being the first player's opening mark.

A player wins the moment they hold all three cells of any row, any column, or either of the two long diagonals, and the game stops there.

Return "A" or "B" for the winner, "Draw" when the board fills with no winner, or "Pending" when the game is unfinished.

Examples

Example 1

Input
moves = [[0, 0], [1, 0], [0, 1], [1, 1], [0, 2]]
Output
"A"

The first player takes the whole top row on their third mark, which wins.

Example 2

Input
moves = [[0, 0], [0, 1], [1, 1], [0, 2], [2, 2]]
Output
"A"

The first player's three marks run down the long diagonal from the top left.

Example 3

Input
moves = [[0, 0], [1, 1]]
Output
"Pending"

Only two marks have been made, so the game is still going.

Constraints

  • 1 <= moves.length <= 9
  • moves[i].length == 2
  • 0 <= moves[i][0] <= 2
  • 0 <= moves[i][1] <= 2
  • No cell is played twice
  • The plays follow the rules of the game

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def tictactoe(moves: list[list[int]]) -> str:
Java
public String tictactoe(int[][] moves)