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'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.
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.
Time Complexity: O(1)
Space Complexity: O(1)
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.
Time Complexity: O(1)
Space Complexity: O(1)
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).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Calculate Parity for Each Coordinate | Time Complexity: O(1) |
| Use Precomputed Board Pattern | Time Complexity: O(1) |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Calculate Parity for Each Coordinate | O(1) | O(1) | Best general solution; minimal computation and no extra memory |
| Precomputed Board Pattern | O(1) | O(1) | Useful when demonstrating the board structure or when a grid representation already exists |
LeetCode 3274 | easy | Check if Two Chessboard Squares Have the Same Color | Python code explanation • Techtonic Knights • 1,199 views views
Watch 9 more video solutions →Practice Check if Two Chessboard Squares Have the Same Color with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor