LeetCode Problem

How to Solve Check if Two Chessboard Squares Have the Same Color

To solve this problem, immediately map the letters of the coordinates to column numbers and sum them with the row numbers. If the sum of each square modulo 2 matches, the squares share the same color. This uses a combination of string parsing and simple arithmetic to directly determine black or white squares.

GhostInterview Help

Need help with Check if Two Chessboard Squares Have the Same Color without spending extra time grinding it?

GhostInterview can read Check if Two Chessboard Squares Have the Same Color from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3274Math plus StringReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Math plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, immediately map the letters of the coordinates to column numbers and sum them with the row numbers. If the sum of each square modulo 2 matches, the squares share the same color. This uses a combination of string parsing and simple arithmetic to directly determine black or white squares.

Problem Statement

You are given two strings, coordinate1 and coordinate2, each representing a square on a standard 8x8 chessboard with columns 'a' to 'h' and rows '1' to '8'. Your task is to decide whether both squares are the same color.

Return true if the squares share the same color and false if they differ. For example, coordinate1 = "a1" and coordinate2 = "c3" both point to black squares, so the answer is true. This problem combines string parsing with arithmetic logic for fast evaluation.

Examples

Example 1

Input: coordinate1 = "a1", coordinate2 = "c3"

Output: true

Both squares are black.

Example 2

Input: coordinate1 = "a1", coordinate2 = "h3"

Output: false

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'

Solution Approach

Map Columns to Numbers

Convert the first character of each coordinate from 'a'-'h' to integers 1-8, then combine with the row number from the second character. This creates a numeric representation for each square.

Check Parity for Color

Sum the column and row numbers for each square and take modulo 2. Squares with the same modulo result share the same color. This directly applies the chessboard color pattern rule.

Return Boolean Result

Compare the parity results of both squares. Return true if they match and false otherwise. This gives an O(1) solution using simple arithmetic and string indexing.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The solution runs in constant time and uses constant space because it only processes two fixed-length strings and performs a few arithmetic operations without extra data structures.

What Interviewers Usually Probe

  • Checks if you understand mapping characters to numbers for arithmetic checks.
  • Tests ability to combine string parsing with modular arithmetic.
  • Observes if you optimize for O(1) rather than iterating the board.

Common Pitfalls or Variants

Common pitfalls

  • Confusing row and column indices when converting letters to numbers.
  • Forgetting to apply modulo 2, leading to incorrect color comparisons.
  • Assuming a fixed board starting color without verifying the coordinate sums.

Follow-up variants

  • Determine if multiple pairs of squares all share the same color efficiently.
  • Check the color difference for a variable-sized chessboard using the same parity logic.
  • Return the color ('black' or 'white') instead of a boolean to generalize the solution.

How GhostInterview Helps

  • Automatically converts coordinates into numeric values and applies parity logic.
  • Highlights failure points like incorrect index mapping or modulo mistakes.
  • Provides immediate boolean feedback and step-by-step explanation for this math-plus-string pattern.

Topic Pages

FAQ

What does 'Check if Two Chessboard Squares Have the Same Color' mean in code terms?

It means converting each coordinate to numeric row and column, summing them, and comparing the parity to decide if the squares share the same color.

Can this approach handle any valid chessboard coordinates?

Yes, as long as the inputs are in the range 'a'-'h' for columns and '1'-'8' for rows, the parity method works reliably.

Why is modulo 2 used in this problem?

Because the color alternates every square, so the sum of row and column being even or odd directly determines black or white.

Is this solution efficient for multiple queries?

Yes, each check is O(1), so multiple coordinate pairs can be processed quickly without iterating the board.

What are common mistakes to avoid?

Mixing up row and column mapping, forgetting to convert letters to numbers, and skipping the modulo operation are frequent errors.

GhostInterview Solver

Need direct help with Check if Two Chessboard Squares Have the Same Color instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Check if Two Chessboard Squares Have the Same Color from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.