Check if Two Chessboard Squares Have the Same Color solution and explanation
9 more videos on this problem
LeetCode 3274 | easy | Check if Two Chessboard Squares Have the Same Color | Python code explanationTechtonic Knights · 2:21 · 1K views
3274 Check if Two Chessboard Squares Have the Same Color || 5 Liners || How to 🤔 in Interview|| MathAyush Rao · 3:41 · 568 views
Check if Two Chessboard Squares Have the Same Color |LeetCode Weekly Contest 413 | Leetcode Solutioncodi · 0:37 · 541 views
3274. Check if Two Chessboard Squares Have the Same Color (Leetcode Easy)Programming Live with Larry · 5:47 · 429 views
leetcode 3274: check if 2 chess board squares have same color: python solutionleetcode blind 75 · 2:49 · 239 views
3274. Check if Two Chessboard Squares Have the Same Color | Leetcode | EditorialCodeLearnCode · 4:34 · 192 views
Check if Two Chessboard Squares Have the Same Color || Weekly Contest 413ExpertFunda · 2:58 · 179 views
3274. Check if Two Chessboard Squares Have the Same Color || Weekly Contest 413 || Brute ForceMohit Siwal · 4:02 · 89 views
Leetcode 3274 Check if Two Chessboard Squares Have the Same Color - In Hindi ExplanationAlgorithmicIQ · 10:51 · 33 views
LeetCode #3274 - Check If Two Chessboard Squares Have The Same Color | C++ SolutionAnsh Patel · 3:44 · 16 views
The problem
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.
def has_same_color(coordinate1, coordinate2):
return (ord(coordinate1[0]) - ord('a') + int(coordinate1[1]) - 1) % 2 == (
ord(coordinate2[0]) - ord('a') + int(coordinate2[1]) - 1) % 2class Solution {
public boolean hasSameColor(String coordinate1, String coordinate2) {
return ((coordinate1.charAt(0) - 'a' + coordinate1.charAt(1) - '1') % 2) ==
((coordinate2.charAt(0) - 'a' + coordinate2.charAt(1) - '1') % 2);
}
}#include <string>
bool hasSameColor(std::string coordinate1, std::string coordinate2) {
return ((coordinate1[0] - 'a' + coordinate1[1] - '1') % 2) ==
((coordinate2[0] - 'a' + coordinate2[1] - '1') % 2);
}function hasSameColor(coordinate1, coordinate2) {
return ((coordinate1.charCodeAt(0) - 'a'.charCodeAt(0) + +coordinate1[1] - 1) % 2) ===
((coordinate2.charCodeAt(0) - 'a'.charCodeAt(0) + +coordinate2[1] - 1) % 2);
}#include <stdbool.h>
bool hasSameColor(char *coordinate1, char *coordinate2) {
return ((coordinate1[0] - 'a' + coordinate1[1] - '1') % 2) ==
((coordinate2[0] - 'a' + coordinate2[1] - '1') % 2);
}public class Solution {
public bool HasSameColor(string coordinate1, string coordinate2) {
return ((coordinate1[0] - 'a' + coordinate1[1] - '1') % 2) ==
((coordinate2[0] - 'a' + coordinate2[1] - '1') % 2);
}
}In Python, we convert the column character using the ord function and subtract the baseline 'a' to obtain the index. We then calculate and compare parity.
Time Complexity: O(1)
Space Complexity: O(1)
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.
def has_same_color(coordinate1, coordinate2):
board = [[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]]
x1, y1 = ord(coordinate1[0]) - ord('a'), int(coordinate1[1]) - 1
x2, y2 = ord(coordinate2[0]) - ord('a'), int(coordinate2[1]) - 1
return board[x1][y1] == board[x2][y2]class Solution {
public boolean hasSameColor(String coordinate1, String coordinate2) {
int[][] board = {{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
int x1 = coordinate1.charAt(0) - 'a', y1 = coordinate1.charAt(1) - '1';
int x2 = coordinate2.charAt(0) - 'a', y2 = coordinate2.charAt(1) - '1';
return board[x1][y1] == board[x2][y2];
}
}#include <string>
bool hasSameColor(std::string coordinate1, std::string coordinate2) {
int board[8][8] = {{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
int x1 = coordinate1[0] - 'a', y1 = coordinate1[1] - '1';
int x2 = coordinate2[0] - 'a', y2 = coordinate2[1] - '1';
return board[x1][y1] == board[x2][y2];
}function hasSameColor(coordinate1, coordinate2) {
const board = [
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0]
];
const x1 = coordinate1.charCodeAt(0) - 'a'.charCodeAt(0);
const y1 = +coordinate1[1] - 1;
const x2 = coordinate2.charCodeAt(0) - 'a'.charCodeAt(0);
const y2 = +coordinate2[1] - 1;
return board[x1][y1] === board[x2][y2];
}#include <stdbool.h>
bool hasSameColor(char *coordinate1, char *coordinate2) {
int board[8][8] = {{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
int x1 = coordinate1[0] - 'a', y1 = coordinate1[1] - '1';
int x2 = coordinate2[0] - 'a', y2 = coordinate2[1] - '1';
return board[x1][y1] == board[x2][y2];
}public class Solution {
public bool HasSameColor(string coordinate1, string coordinate2) {
int[,] board = new int[8,8] {{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}};
int x1 = coordinate1[0] - 'a';
int y1 = coordinate1[1] - '1';
int x2 = coordinate2[0] - 'a';
int y2 = coordinate2[1] - '1';
return board[x1, y1] == board[x2, y2];
}
}In Python, this solution uses a hardcoded board pattern to check the color.
Time Complexity: O(1)
Space Complexity: O(1)
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).
function checkTwoChessboards(coordinate1: string, coordinate2: string): boolean {
const x = coordinate1.charCodeAt(0) - coordinate2.charCodeAt(0);
const y = coordinate1.charCodeAt(1) - coordinate2.charCodeAt(1);
return (x + y) % 2 === 0;
}func checkTwoChessboards(coordinate1 string, coordinate2 string) bool {
x := coordinate1[0] - coordinate2[0]
y := coordinate1[1] - coordinate2[1]
return (x+y)%2 == 0
}Complexity comparison
| Approach | Complexity |
|---|---|
| Calculate Parity for Each Coordinate | Time Complexity: O(1) |
| Use Precomputed Board Pattern | Time Complexity: O(1) |
| Mathematics | – |
Complexity analysis
| 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 |
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).
