Problem 1275

Abstract / Summary

Given a list of moves in a tic tac toe game determine if a player has won.

Note

The full problem description, including helpful examples, can be found on LeetCode.

C++ 1275.cpp

class Solution {
public:
    string tictactoe(vector<vector<int>>& moves) {
        char board[3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' }};
        for (auto i = 0; i < moves.size(); i++)
        {
            auto r = moves[i][0];
            auto c = moves[i][1];
            board[r][c] = (i & 1) ? 'B' : 'A';
        }

        for (int i = 0; i < 3; i++)
        {
            if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][0] == board[i][2])
                return string(1, board[i][0]);
            if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[0][i] == board[2][i])
                return string(1, board[0][i]);
            if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2])
                return string(1, board[0][0]);
            if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0])
                return string(1, board[0][2]);
        }

        if (moves.size() == 9)
            return "Draw";

        return "Pending";
    }
};
29 lines | 1099 bytes | submitted August 7, 2023 | GitHub   tab size 4

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 55% (300k+ submissions).

Typical patterns to solve this problem:
  • Array
  • Hash Table
  • Matrix
  • Simulation
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The presented implementation determines the result of a Tic-Tac-Toe game given a sequence of moves. It builds an internal 3×3 board, fills it according to the move order, then checks all possible winning lines. Depending on the board state it returns "A" for player A, "B" for player B, "Draw" when the board is full without a winner, or "Pending" when moves remain.

Board Construction

The algorithm starts by allocating a fixed-size character matrix initialized with blanks.

char board[3][3] = { { ' ', ' ', ' ' },
                     { ' ', ' ', ' ' },
                     { ' ', ' ', ' ' } };

A simple loop iterates over the moves vector. For each move the row and column indices are extracted, and the board cell is set to 'A' or 'B' based on the parity of the move index.

for (auto i = 0; i < moves.size(); i++)
{
    auto r = moves[i][0];
    auto c = moves[i][1];
    board[r][c] = (i & 1) ? 'B' : 'A';
}

The use of the bitwise & operator to test the least-significant bit provides a fast way to alternate between the two players without a conditional branch.

Winning Line Detection

After the board is populated, a single loop runs three times, using the loop variable as an index for rows and columns. Within each iteration the code checks four distinct patterns:

  1. Horizontal line in row i.
  2. Vertical line in column i.
  3. The main diagonal (checked repeatedly but harmless).
  4. The anti-diagonal (also checked repeatedly).

The checks are short-circuiting: they first verify that the starting cell is not blank, then compare the three cells for equality. When a winning line is found, the function returns a one-character string containing the winner’s symbol.

for (int i = 0; i < 3; i++)
{
    if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][0] == board[i][2])
        return string(1, board[i][0]);

    if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[0][i] == board[2][i])
        return string(1, board[0][i]);

    if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2])
        return string(1, board[0][0]);

    if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0])
        return string(1, board[0][2]);
}

Because the board size is constant, the repeated diagonal checks do not affect overall efficiency, and they keep the code layout uniform.

Determining Draw or Pending

If no winner is detected, the function examines the total number of moves. A full board (nine moves) indicates a draw; otherwise the game is still in progress.

if (moves.size() == 9)
    return "Draw";

return "Pending";

Edge Cases Handled

The implementation gracefully handles the situation where the move list is shorter than nine, returning "Pending" when no winning line exists. It also correctly identifies a win that occurs before the board is filled, because the win detection runs after all moves have been placed, regardless of move count. The alternating player assignment using the index parity ensures that even if the input contains an odd number of moves, the correct player symbols are placed.

When the input contains exactly nine moves without a winner, the final conditional returns "Draw". The code also tolerates an empty move list: the board remains blank, the win checks find no matches, and the size check falls through to "Pending".

Performance Optimizations

The algorithm uses a statically sized array for the board, avoiding dynamic allocation and the overhead of container classes. The board is populated in a single pass over the input vector, and the win detection loop runs a fixed three iterations, each performing only a handful of comparisons. The parity test (i & 1) replaces a modulo operation, offering a marginally faster determination of the current player. Returning a string constructed from a single character (string(1, board[i][0])) avoids creating larger temporary objects.

Overall, the solution operates with minimal memory footprint and constant-time work for each phase, making it well-suited for the constrained 3×3 Tic-Tac-Toe board.

written by openai gpt

ELI5 - Simplified Explanation

Imagine a wide, empty tic-tac-toe board drawn on a piece of paper. This code acts like a tiny robot that watches two friends, named A and B, take turns writing their letters on the grid.

First, the robot clears off any old marks so the board is completely empty. Then, it looks at a list of every move the friends make. For each move, the robot knows exactly which square the friend chose, and it draws their special letter, either an A or a B, right where it belongs.

Once the moves are finished, the robot looks at the grid to see if anyone won. It checks all the rows, all the columns, and both of the long diagonal lines that cross through the center. If it sees three matching letters in a row, it tells everyone who the winner is. If all the boxes are full and nobody managed to line up three letters, the robot says it is a draw, which is just like a tie. If the board still has empty spots and no one has won yet, the robot tells us that the game is still going on.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 26 | median = 7 | μ = 6.88 | σ = 1.19

Complexity Analysis

not analyzed yet

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Java Go TS
Progiez C++ Python Java
github BigEggStudy C#
black-shadows C++
charles-wangkai Java
DimitrisJim C
everthis JS
fartem Ruby
fsq C++
Garvit244 Python
halfrost Go
JoshCrozier JS
keineahnung2345 C++
lkwq007 Python
suxrobGM C#
varunu28 Java
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, Python, TypeScript
Progiez C++, Java, Python
BigEggStudy C#
black-shadows C++, C++
charles-wangkai Java
DimitrisJim C
everthis JavaScript
fartem Ruby
fsq C++
Garvit244 Python
halfrost Go
JoshCrozier JavaScript
keineahnung2345 C++
lkwq007 Python
suxrobGM C#
varunu28 Java
29 links | 18 authors | 9 languages

Videos

5 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

39 easy 31 medium 1 hard
72 solutions   August 7, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality