Skip to content
← All posts

Determine Color of a Chessboard Square: Quick Math Trick

4 min read
leetcodeproblemeasystringsmath

LeetCode 1812. Determine Color of a Chessboard Square gives you a string like "a1" or "h3" representing a square on a standard chessboard. You need to return true if the square is white and false if it is black.

The key insight: you do not need to build or store a chessboard. The color of any square is fully determined by the parity (even or odd) of its column number plus its row number.

Chessboard square colorsa1c3e6abcdefgh12345678Black (sum is even)White (sum is odd)HighlightedHighlighted squaresa1: 1+1=2 (even, black)c3: 3+3=6 (even, black)e6: 5+6=11 (odd, white)
A chessboard with columns a-h and rows 1-8. When column number + row number is even, the square is black. When odd, it is white.

Problem statement

You are given a string coordinates that represents a chessboard square. The first character is a letter from 'a' to 'h' (the column), and the second character is a digit from '1' to '8' (the row). Return true if the square is white, or false if it is black.

  • "a1" is black
  • "h3" is black
  • "c7" is white

Approach: Parity check

On a standard chessboard, colors alternate. If you number the columns a=1, b=2, ..., h=8 and keep the row as its digit, you get a grid of coordinates. The pattern is simple: when the sum of column number and row number is even, the square is black. When the sum is odd, the square is white.

This means you can solve the problem with a single arithmetic expression. No loops, no conditionals beyond the final comparison.

def square_is_white(coordinates: str) -> bool:
    col = ord(coordinates[0]) - ord('a') + 1
    row = int(coordinates[1])
    return (col + row) % 2 != 0

The function converts the column letter to a number (a=1, b=2, ...), grabs the row digit, adds them together, and checks if the result is odd. That is all there is to it.

Visual walkthrough: coordinates = "c3"

Let's trace through the solution step by step to see why parity works.

Tracing squareIsWhite("c3"):

Step 1: Extract column letter and row digit

input ="c3"
column letter ="c"
row digit ="3"

The first character is the column (a-h). The second character is the row (1-8).

Step 2: Convert column letter to a number

col =ord("c") - ord("a") + 1 = 3
row =int("3") = 3

a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8. Using ord() gives us the character code, and subtracting ord('a') then adding 1 converts to 1-indexed.

Step 3: Compute col + row and check parity

sum =3 + 3 = 6
parity =6 % 2 = 0 (even)

If the sum is even, the square is black. If odd, the square is white. This works because adjacent squares always alternate in color.

Step 4: Sum is even, so c3 is black. Return False.

The function returns False because the square is not white. For a white square like "e6" (5+6=11, odd), it would return True.

The column 'c' maps to 3, the row is 3, and 3+3=6 is even, so the square is black and we return False. If we tried "e6" instead, we would get 5+6=11 (odd), so we would return True for white.

Why does parity work?

Think about how a chessboard is colored. Start at a1 (black). Moving one square to the right flips the color. Moving one square up also flips the color. Each move changes either the column or the row by 1, toggling the parity of the sum. So any square where col + row is even shares the same color as a1 (black), and any square where it is odd has the opposite color (white).

This same parity trick shows up in grid-based problems beyond chess. Whenever you need to color a grid in a checkerboard pattern or determine if two cells are on the same "color," check if the sum of their coordinates has the same parity.

Complexity analysis

ApproachTimeSpace
Parity checkO(1)O(1)

The input is always exactly two characters, so every operation (character conversion, addition, modulo) takes constant time and uses constant space.

Building blocks

This problem decomposes into two reusable pieces that appear across many LeetCode problems.

1. Character-to-number conversion with ord()

Converting a lowercase letter to its position in the alphabet using ord(ch) - ord('a') + 1. This same technique appears in Excel column problems, Caesar cipher variants, and many string-processing challenges.

col = ord(coordinates[0]) - ord('a') + 1

2. Parity check with modulo

Using % 2 to determine whether a value is even or odd. Parity checks are fundamental in problems involving alternating patterns, checkerboard grids, and bit manipulation.

is_odd = (col + row) % 2 != 0

On CodeBricks, you drill each building block separately so that when they appear together in a problem like this, you recognize both pieces instantly.

Edge cases

  • Corner squares: "a1" (1+1=2, even, black) and "h8" (8+8=16, even, black). Both corners on the a1-h8 diagonal are black, confirming the parity rule.
  • First row vs. first column: "a1" is black but "b1" is white (2+1=3, odd). Make sure you convert the column letter correctly and do not mix up zero-indexed vs. one-indexed values.
  • All white squares on rank 1: "b1", "d1", "f1", "h1" all have even column numbers, so col+row is odd, confirming they are white.
  • Single formula handles everything: There are no special cases or boundary conditions to worry about. The parity formula works uniformly for all 64 squares.

From understanding to recall

You can read through this solution in under a minute and understand every line. But understanding is not the same as being able to reproduce it under interview pressure two weeks from now.

The gap between understanding and recall is where spaced repetition comes in. CodeBricks breaks this problem into its building blocks (character-to-number conversion with ord(), parity check with modulo) and drills them at increasing intervals. You type each piece from scratch, building real muscle memory. After a few review cycles, the parity pattern becomes automatic rather than something you have to rederive.

Related posts

  • Roman to Integer - Another character-to-value mapping problem where you process a string one character at a time
  • Palindrome Number - A math-based approach to a number property check, similar in spirit to using parity here
  • Valid Sudoku - Grid-based validation that relies on indexing rows, columns, and sub-boxes by coordinate