Determine Color of a Chessboard Square solution and explanation
9 more videos on this problem
LeetCode-1812.Determine Color of a chess board squareJourney with AI: Unlocked · 3:58 · 1K views
1812. Determine Color of a Chessboard Square (Leetcode Easy)Programming Live with Larry · 2:46 · 1K views
Leetcode 1812 Determine Color of a Chessboard SquareMath Geeks · 2:36 · 1K views
Solution of LeetCode 1812 | Determine Color of a Chessboard SquareInterCoding · 6:36 · 626 views
Determine Color of a Chessboard Square #leetcode ChallengeCodeVia · 6:30 · 337 views
LeetCode 1812 | Determine Color of a Chessboard Square | ASCII | JavaSleepy Cracker · 1:28 · 284 views
LeetCode Çözümleri | Determine Color of a Chessboard SquareSoner Gönül · 4:52 · 169 views
Crack LeetCode Q.1812: Determine Color of a Chessboard Square (Java Solution!)CodoPhilic · 6:06 · 131 views
Determine Color of a Chessboard Square | Leetcode 1812 | Approach + Code with explanation.Pirai's Academy · 9:23 · 48 views
1812. Determine Color of a Chessboard Square - Javascript - Time: O(n) Space: O(1) - LeetcodeAaron Sadino Coding · 5:35 · 38 views
The problem
You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.
Example 1:
Input: coordinates = "a1" Output: false Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.
Example 2:
Input: coordinates = "h3" Output: true Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.
Example 3:
Input: coordinates = "c7" Output: false
Constraints:
coordinates.length == 2'a' <= coordinates[0] <= 'h''1' <= coordinates[1] <= '8'
Approach overview
Problem Overview: A chessboard coordinate like "a1" or "h3" is given. The first character represents the column (a–h) and the second represents the row (1–8). The task is to determine whether that square is white or black on a standard chessboard.
Approach 1: Index-Based Calculation (Time: O(1), Space: O(1))
Convert the column letter into a numeric index using ASCII arithmetic (column - 'a' + 1). Convert the row character into an integer. Once both coordinates are numeric, add them together. On a chessboard, squares with an even sum are black and squares with an odd sum are white (or vice versa depending on indexing choice, but the parity relationship stays consistent). This approach relies on simple math operations and minimal string parsing.
Approach 2: Parity of Coordinates (Time: O(1), Space: O(1))
Observe the pattern of a chessboard: colors alternate every step horizontally and vertically. Convert the column letter and row digit into integers, then check whether both have the same parity (both even or both odd). If the parity matches, the square has one color; if different, it has the other. The key insight is that color changes whenever you move one step in either direction, so parity fully determines the color. This approach expresses the logic directly with a parity check such as (col + row) % 2.
Recommended for interviews: The parity-based approach is what most interviewers expect. It shows you recognized the alternating chessboard pattern and reduced the problem to a constant-time arithmetic check. The index-based calculation demonstrates the same idea but makes the coordinate conversion step explicit. Both run in O(1) time and O(1) space.
Approach 1: Index-Based Calculation
The chessboard can be considered as an 8x8 grid where each square is identified by a combination of a letter (column 'a' to 'h') and a number (row '1' to '8').
One method to determine if the square is black or white is to map these coordinates to indices: columns can be mapped to numbers (a=1, b=2, ..., h=8), and rows are directly represented by numbers (1 to 8). The color of a square can be determined by the sum of these mapped values. If the sum is even, the square is black; if odd, it is white.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
column = ord(coordinates[0]) - ord('a') + 1
row = int(coordinates[1])
return (column + row) % 2 != 0class Solution {
public boolean squareIsWhite(String coordinates) {
int column = coordinates.charAt(0) - 'a' + 1;
int row = coordinates.charAt(1) - '0';
return (column + row) % 2 != 0;
}
}#include <string>
class Solution {
public:
bool squareIsWhite(std::string coordinates) {
int column = coordinates[0] - 'a' + 1;
int row = coordinates[1] - '0';
return (column + row) % 2 != 0;
}
};function squareIsWhite(coordinates) {
const column = coordinates.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
const row = parseInt(coordinates[1]);
return (column + row) % 2 !== 0;
}#include <stdbool.h>
bool squareIsWhite(char * coordinates) {
int column = coordinates[0] - 'a' + 1;
int row = coordinates[1] - '0';
return (column + row) % 2 != 0;
}public class Solution {
public bool SquareIsWhite(string coordinates) {
int column = coordinates[0] - 'a' + 1;
int row = coordinates[1] - '0';
return (column + row) % 2 != 0;
}
}The Python solution uses ord() to convert the letter to a number, and the digit part of the string to an integer. Odd sums indicate a white square.
Time Complexity: O(1), the work done is constant.
Space Complexity: O(1), only a few primitive variables are used.
Approach 2: Parity of Coordinates
Another approach is to explicitly define the parity rule. We analyze the position and discern that squares in the same diagonal alternation such as (0,0), (1,1), ..., are of the same color. Here, shifting the ASCII value shifts the problem to decide via binary even-odd pairing.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
return (ord(coordinates[0]) + ord(coordinates[1])) % 2 != 0class Solution {
public boolean squareIsWhite(String coordinates) {
return (coordinates.charAt(0) + coordinates.charAt(1)) % 2 != 0;
}
}#include <string>
class Solution {
public:
bool squareIsWhite(std::string coordinates) {
return (coordinates[0] + coordinates[1]) % 2 != 0;
}
};function squareIsWhite(coordinates) {
return (coordinates.charCodeAt(0) + coordinates.charCodeAt(1)) % 2 !== 0;
}#include <stdbool.h>
bool squareIsWhite(char * coordinates) {
return (coordinates[0] + coordinates[1]) % 2 != 0;
}public class Solution {
public bool SquareIsWhite(string coordinates) {
return (coordinates[0] + coordinates[1]) % 2 != 0;
}
}In Python, ord() function directly taps into the ASCII encoding, making character handling streamlined and efficient for determining even or odd summations.
Time Complexity: O(1), interpreting character values is done quickly.
Space Complexity: O(1), only space for fixed, little data storage is needed.
Approach 3: Pattern Recognition
Observing the chessboard, we find that two squares (x_1, y_1) and (x_2, y_2) with the same color satisfy that both x_1 + y_1 and x_2 + y_2 are either odd or even.
Therefore, we can get the corresponding coordinates (x, y) from coordinates. If x + y is odd, the square is white, and we return true; otherwise, we return false.
The time complexity is O(1), and the space complexity is O(1).
function squareIsWhite(coordinates: string): boolean {
return ((coordinates.charCodeAt(0) + coordinates.charCodeAt(1)) & 1) === 1;
}func squareIsWhite(coordinates string) bool {
return (coordinates[0]+coordinates[1])%2 == 1
}impl Solution {
pub fn square_is_white(coordinates: String) -> bool {
let s = coordinates.as_bytes();
((s[0] + s[1]) & 1) == 1
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Index-Based Calculation | Time Complexity: O(1), the work done is constant. |
| Parity of Coordinates | Time Complexity: O(1), interpreting character values is done quickly. |
| Pattern Recognition | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Index-Based Calculation | O(1) | O(1) | Clear and explicit conversion from chess notation to numeric coordinates. |
| Parity of Coordinates | O(1) | O(1) | Preferred in interviews; directly uses parity pattern of a chessboard. |
Frequently asked questions
Is Determine Color of a Chessboard Square easy or hard?
Determine Color of a Chessboard Square is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge is recognizing the alternating chessboard pattern and converting the coordinate string into numeric indices.
Determine Color of a Chessboard Square Python/Java solution
In both Python and Java, convert the column character to a numeric index and add it to the row digit. Then return whether the sum is odd or even. The logic is identical across languages and runs in O(1) time with O(1) extra space.
How to solve Determine Color of a Chessboard Square in O(1)?
Convert the column character to an index using ASCII math and parse the row digit as an integer. Compute the sum of the two values and check its parity. If the sum is even the square is one color, if odd it is the other. This requires only constant operations, giving O(1) time and O(1) space.
What is the best approach for Determine Color of a Chessboard Square?
The most common approach checks the parity of the coordinate indices. Convert the column letter to a number (a=1 through h=8), add it to the row number, and evaluate (row + column) % 2. The parity determines the color. This runs in O(1) time and O(1) space.
Is Determine Color of a Chessboard Square asked at Google/Amazon/Meta?
Problems like this appear in coding screens that test basic pattern recognition and coordinate reasoning. While not a common hard interview problem at Google, Amazon, or Meta, similar parity or grid-coloring questions appear in early screening rounds and coding practice sets.
What data structure is used in Determine Color of a Chessboard Square?
No complex data structure is required. The solution relies on simple string parsing and arithmetic operations. The key idea comes from math and coordinate parity rather than arrays, hash maps, or trees.
What is the time complexity of Determine Color of a Chessboard Square?
The problem is solved in constant time. Only a few character conversions and a parity check are required, so the time complexity is O(1). The algorithm also uses O(1) space since no additional data structures are needed.
