Skip to main content

Check if Two Chessboard Squares Have the Same Color - Solution & Explanation

EasyMathString13 min readAsked at: Amazon, Meta, Bloomberg
Practice this problem

Problem Statement

You are given two strings, coordinate1 and coordinate2, representing the coordinates of a square on an 8 x 8 chessboard.

Below is the chessboard for reference.

Return true if these two squares have the same color and false otherwise.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).

 

Example 1:

Input: coordinate1 = "a1", coordinate2 = "c3"

Output: true

Explanation:

Both squares are black.

Example 2:

Input: coordinate1 = "a1", coordinate2 = "h3"

Output: false

Explanation:

Square "a1" is black and "h3" is white.

 

Constraints:

  • coordinate1.length == coordinate2.length == 2
  • 'a' <= coordinate1[0], coordinate2[0] <= 'h'
  • '1' <= coordinate1[1], coordinate2[1] <= '8'

Approach Overview

Problem Overview: You receive two chessboard coordinates such as a1 or h8. Each coordinate represents a square on a standard 8x8 chessboard. The task is to determine whether both squares have the same color (both black or both white).

Approach 1: Calculate Parity for Each Coordinate (Time: O(1), Space: O(1))

A chessboard alternates colors every square. This means the color depends on the parity of its row and column index. Convert the column letter (a–h) to a numeric index and combine it with the row digit. If (row + column) % 2 is the same for both squares, they share the same color. The insight comes from math: squares with even parity map to one color and odd parity map to the other. Parsing the coordinate is a simple string operation, so the overall computation is constant time.

This approach is the most direct and efficient solution. You convert each coordinate once, compute parity, and compare results. No board representation or additional memory is required.

Approach 2: Use Precomputed Board Pattern (Time: O(1), Space: O(1))

Another option is to model the chessboard color pattern directly. Precompute an 8Ɨ8 grid where each cell stores either black or white according to the standard alternating layout. Convert each coordinate to its row and column index, then look up the stored color in the grid. If both lookups return the same value, the squares match in color.

This method trades a tiny constant amount of memory for conceptual simplicity. Instead of reasoning about parity, the solution relies on a predefined board layout and simple indexing. It still runs in constant time since the board size never changes.

Recommended for interviews: The parity calculation approach is what interviewers typically expect. It demonstrates that you recognize the mathematical pattern behind the chessboard rather than simulating the board itself. Showing the board-pattern method first can help explain the alternating structure, but the parity solution proves you can reduce the problem to a minimal O(1) computation.

Approach 1: Calculate Parity for Each Coordinate

The idea is that on a chessboard, the color of a square can be determined by considering the parity (even or odd) of the sum of the column index and row number.

If we treat the columns as numbers (a=1, b=2, ..., h=8), then we find the sum of the column number and row number for each coordinate. If both coordinates have the same parity (both even or both odd), then they have the same color; otherwise, they differ.

This C function calculates the 'color parity' by converting the column letter into an index using 'a' as base, then adds the numeric row. We then check if both coordinates have the same parity.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Use Precomputed Board Pattern

Another approach is to precompute the colors in a board pattern represented as a 2D array of binary values (0 for black, 1 for white) and compare values at the given coordinates.

This C function uses a predefined chessboard pattern array to find the color at given coordinates and compare them.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Mathematics

We calculate the differences in the x-coordinates and y-coordinates of the two points. If the sum of these differences is even, then the colors of the squares at these two coordinates are the same; otherwise, they are different.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Calculate Parity for Each Coordinate

Time Complexity: O(1)
Space Complexity: O(1)

Use Precomputed Board Pattern

Time Complexity: O(1)
Space Complexity: O(1)

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Calculate Parity for Each CoordinateO(1)O(1)Best general solution; minimal computation and no extra memory
Precomputed Board PatternO(1)O(1)Useful when demonstrating the board structure or when a grid representation already exists

Video Solution

LeetCode 3274 | easy | Check if Two Chessboard Squares Have the Same Color | Python code explanation • Techtonic Knights • 1,201 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check if Two Chessboard Squares Have the Same Color easy or hard?
This problem is categorized as Easy because it requires only basic string parsing and simple mathematical reasoning. Recognizing the alternating color pattern and using parity reduces the entire task to a constant-time calculation.
Check if Two Chessboard Squares Have the Same Color Python/Java solution
In Python or Java, convert the column character using ASCII arithmetic (for example, column = square.charAt(0) - 'a' + 1) and extract the row digit. Compute (row + column) % 2 for each square and compare the results. Matching parity means the squares have the same color.
How to solve Check if Two Chessboard Squares Have the Same Color in O(n)?
An O(n) approach is unnecessary because the board size is fixed. The correct solution computes the parity of each coordinate using simple math and runs in constant time O(1). Any solution that iterates across the board would be less efficient than the direct parity calculation.
What is the best approach for Check if Two Chessboard Squares Have the Same Color?
The parity calculation approach is the most efficient and commonly expected solution. Convert the column letter to a number, add it to the row value, and check the parity using (row + column) % 2. If both squares produce the same parity, they share the same color. This method runs in O(1) time and O(1) space.
Is Check if Two Chessboard Squares Have the Same Color asked at Google/Amazon/Meta?
Problems involving coordinate parity and grid coloring patterns appear frequently in coding screens at large tech companies. While this exact problem may not always appear, the underlying concept of using math or parity to simplify grid problems is commonly tested.
What data structure is used in Check if Two Chessboard Squares Have the Same Color?
The optimal approach does not require a complex data structure. It relies on simple arithmetic after parsing the coordinate string. Some implementations may use a small 8x8 array to represent the board, but this is optional and not necessary for the constant-time parity solution.
What is the time complexity of Check if Two Chessboard Squares Have the Same Color?
The optimal solution runs in O(1) time because it only converts two coordinates and performs a few arithmetic operations. No loops or data structures proportional to input size are required. Space complexity is also O(1).

Ready to solve this problem?

Practice Check if Two Chessboard Squares Have the Same Color with our built-in code editor and test cases.

Practice on FleetCode