Determine Color of a Chessboard Square
Problem
You are given a two-character string coordinates representing a chessboard square, where the first character is a column 'a'..'h' and the second is a row '1'..'8'. Return true if the square is white and false if it is black.
coordinates = "c7"falsedef square_is_white(coordinates):
col = ord(coordinates[0]) - ord("a")
row = int(coordinates[1]) - 1
return (col + row) % 2 == 1
function squareIsWhite(coordinates) {
const col = coordinates.charCodeAt(0) - 97;
const row = Number(coordinates[1]) - 1;
return (col + row) % 2 === 1;
}
class Solution {
public boolean squareIsWhite(String coordinates) {
int col = coordinates.charAt(0) - 'a';
int row = coordinates.charAt(1) - '1';
return (col + row) % 2 == 1;
}
}
bool squareIsWhite(string coordinates) {
int col = coordinates[0] - 'a';
int row = coordinates[1] - '1';
return (col + row) % 2 == 1;
}
Explanation
A chessboard's colors follow a perfect checkerboard parity: stepping one square in any direction flips the color. That means the color depends only on whether column + row is even or odd.
First we turn the algebraic coordinate into numbers. The column letter becomes 0..7 via ord(letter) - ord('a'), and the row digit becomes 0..7 via int(digit) - 1.
Now we look at the parity of their sum. By convention the bottom-left square a1 (column 0, row 0, sum 0, even) is black. Each flip toggles parity, so odd sums are white and even sums are black.
So the whole problem collapses to (col + row) % 2 == 1 — no board lookup, no special cases, constant time.
Example: "c7" → column c = 2, row 7 = 6. Sum 2 + 6 = 8 is even, so the square is black and we return false.