Find Winner on a Tic Tac Toe Game - Complete Solution Guide
Find Winner on a Tic Tac Toe Game is LeetCode problem 1275, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 A always places 'X' characters, while the second player B always 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. N
Detailed Explanation
We're tasked with analyzing a sequence of moves in a standard 3x3 Tic-Tac-Toe game and determining the final state: whether Player A (X) won, Player B (O) won, the game ended in a draw, or it's still pending a winner. The `moves` input is an array of `[row, col]` pairs, representing the chronological order of placements. Player A always goes first, placing 'X', then Player B places 'O', and so on. Importantly, we're given the *entire* sequence of moves for a completed or partially completed game, not a dynamic turn-by-turn input. We just need to simulate the given game to its conclusion.
Solution Approach
The provided solution takes a direct, simulation-based approach. It meticulously reconstructs the game board by iterating through the `moves` list. A 3x3 grid, initialized with empty strings, serves as our game board. It alternates placing 'X' for Player A and 'O' for Player B based on whose `turn` it is. After all moves from the input `moves` list have been applied to the `grid`, the solution then performs a comprehensive check for a winner.
Step-by-Step Algorithm
- Step 1: Initialize a 3x3 grid (e.g., a 2D array) to represent the Tic-Tac-Toe board. Initially, all cells are empty.
- Step 2: Iterate through the `moves` list. For each move `[row, col]`, place the appropriate player's mark ('X' or 'O') on the board.
- Step 3: After each move (or after all moves), check for a win condition by examining rows, columns, and diagonals. A player wins if they have three of their marks in a row.
- Step 4: If a win is detected, return the winner ('A' or 'B').
- Step 5: If no win is detected and all squares are filled, return 'Draw'.
- Step 6: If no win is detected and the board is not full, return 'Pending'.
Key Insights
- **Direct Game Simulation on a 2D Array:** The most intuitive and effective way to analyze a fixed-size board game like Tic-Tac-Toe is to explicitly build and maintain its state. Using a `3x3` list of lists (or a 2D array) directly mirrors the game board, allowing straightforward placement of 'X's and 'O's based on the input `moves` sequence.
- **Exhaustive Win Condition Checking:** For a 3x3 Tic-Tac-Toe board, there are only 8 possible winning lines (3 rows, 3 columns, 2 diagonals). The `check_winner` helper function systematically iterates through each of these lines for *both* players. This fixed, small number of checks makes determining a winner efficient and reliable, regardless of the sequence of moves, as it's a constant time operation for a 3x3 grid.
- **Prioritized Outcome Determination:** The final result ("A", "B", "Draw", "Pending") must be determined in a specific order after checking for individual player wins. First, check for Player A's win, then Player B's win. If neither has won, then distinguish between a "Draw" (all 9 squares filled, i.e., `len(moves) == 9`) and "Pending" (fewer than 9 squares filled and no winner). This order of checking is crucial to correctly identify the earliest possible end-state.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Array, Hash Table, Matrix, Simulation.
Companies
Asked at: Qualcomm, Tesla, Zoho.