Watch 10 video solutions for Check if Two Chessboard Squares Have the Same Color, a easy level problem involving Math, String. This walkthrough by Techtonic Knights has 1,199 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |