Check if Two Chessboard Squares Have the Same Color

Easy
#2906Time: O(1) - The time taken does not depend on the input size. It involves a fixed number of character operations, arithmetic calculations, and comparisons.Space: O(1) - Constant extra space is used, as we only need a few variables to store the coordinates and colors.
Patterns
Data structures

Prompt

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'

Approaches

2 approaches with complexity analysis and trade-offs.

This approach involves determining the color of each square individually and then comparing them. We can create a helper function that takes a coordinate string and returns its color, represented numerically (e.g., 0 for black, 1 for white). The main function then calls this helper for both coordinates and checks if the results are identical.

Algorithm

  • Create a helper function, for instance getColor(coordinate), that returns an integer representing the color of a square (e.g., 0 for black, 1 for white).
  • Inside the helper function, parse the input coordinate string into 0-indexed column and row integers. For a coordinate like "c3", the column index would be c - a = 2, and the row index would be 3 - 1 = 2.
  • Calculate the sum of the column and row indices.
  • The color is determined by the parity of this sum. Return (col + row) % 2.
  • In the main function, call this getColor helper for both coordinate1 and coordinate2.
  • Compare the two returned color values. If they are equal, the squares have the same color, so return true. Otherwise, return false.

Walkthrough

The color of a square on a chessboard follows a consistent pattern based on its position. If we represent the columns 'a' through 'h' as numbers 0 through 7 and rows '1' through '8' as 0 through 7, a square at (row, col) is one color if row + col is even, and the other color if row + col is odd. This approach formalizes this logic into a reusable function.

We define a helper method that encapsulates the logic for finding a single square's color. This method parses the coordinate, calculates the sum of its numeric indices, and returns the parity of the sum. The main method then uses this helper to find the color of each of the two given squares and compares them.

class Solution {    /**     * Determines the color of a square, represented as 0 or 1.     * @param coordinate The algebraic notation of the square (e.g., "a1").     * @return 0 if the sum of 0-indexed coordinates is even, 1 otherwise.     */    private int getColor(String coordinate) {        int col = coordinate.charAt(0) - 'a';        int row = coordinate.charAt(1) - '1';        return (col + row) % 2;    }     public boolean squareIsSameColor(String coordinate1, String coordinate2) {        // Get the color of each square and compare them.        return getColor(coordinate1) == getColor(coordinate2);    }}

While this approach is perfectly valid and easy to read, it performs the calculation in two distinct steps. A more streamlined approach could combine these steps.

Complexity

Time

O(1) - The time taken does not depend on the input size. It involves a fixed number of character operations, arithmetic calculations, and comparisons.

Space

O(1) - Constant extra space is used, as we only need a few variables to store the coordinates and colors.

Trade-offs

Pros

  • The logic is very clear and easy to understand.

  • It promotes code reuse by separating the color calculation logic into a helper function.

Cons

  • Slightly less performant as it involves more operations (two separate color calculations) than the most optimal approach.

  • Can be considered verbose for such a simple problem.

Solutions

class Solution {public  boolean checkTwoChessboards(String coordinate1, String coordinate2) {    int x = coordinate1.charAt(0) - coordinate2.charAt(0);    int y = coordinate1.charAt(1) - coordinate2.charAt(1);    return (x + y) % 2 == 0;  }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.