Taro Logo

Determine Color of a Chessboard Square

#9 Most AskedEasy
Topics:
Strings

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'

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Are coordinates always two characters, a file from a to h and a rank from 1 to 8?
  2. Should true mean the square is white and false mean it is black?
  3. Is a1 black, matching a standard chessboard?
  4. Is the coloring always the usual eight-by-eight pattern, with no rotated board?
  5. Will the file letter always be lowercase?

Brute Force Solution

Approach

Look the coordinate up in a table that already records the color of all 64 squares. Each square name maps directly to white or black. Return the stored answer for the square that was requested.

Here's how the algorithm would work step-by-step:

  1. Prepare a table of every square from a1 through h8.
  2. Mark each square white or black according to the standard coloring.
  3. Read the coordinate that was asked for.
  4. Look that name up in the table.
  5. Return the stored color.

Code Implementation

def square_is_white(coordinates):
    # The board map acts as a pre-computed answer key for all 64 squares.
    color_map_for_chess_board = {
        'a1': False, 'a2': True, 'a3': False, 'a4': True, 'a5': False, 'a6': True, 'a7': False, 'a8': True,
        'b1': True, 'b2': False, 'b3': True, 'b4': False, 'b5': True, 'b6': False, 'b7': True, 'b8': False,
        'c1': False, 'c2': True, 'c3': False, 'c4': True, 'c5': False, 'c6': True, 'c7': False, 'c8': True,
        'd1': True, 'd2': False, 'd3': True, 'd4': False, 'd5': True, 'd6': False, 'd7': True, 'd8': False,
        'e1': False, 'e2': True, 'e3': False, 'e4': True, 'e5': False, 'e6': True, 'e7': False, 'e8': True,
        'f1': True, 'f2': False, 'f3': True, 'f4': False, 'f5': True, 'f6': False, 'f7': True, 'f8': False,
        'g1': False, 'g2': True, 'g3': False, 'g4': True, 'g5': False, 'g6': True, 'g7': False, 'g8': True,
        'h1': True, 'h2': False, 'h3': True, 'h4': False, 'h5': True, 'h6': False, 'h7': True, 'h8': False
    }

    # This performs a direct lookup to find the color of the requested square.
    color_of_the_square = color_map_for_chess_board[coordinates]

    # We simply return the color found in our pre-made map.
    return color_of_the_square

Big(O) Analysis

Time Complexity
O(1) – The table always has the same 64 entries, so building it is a fixed amount of work. One key is read and one answer is returned. The cost does not grow with anything the caller can change, because a chessboard coordinate is a fixed two-character name. There is no scan of a larger board.
Space Complexity
O(1) – The table always holds the same 64 squares. Its size does not change from one question to the next. No extra copy of the board is stored. Memory stays constant for every coordinate.

Optimal Solution

Approach

Turn the file letter and the rank digit into two numbers and add them. On a standard board, a square is white exactly when that sum is odd, and black when the sum is even. Return true only for the odd sums.

Here's how the algorithm would work step-by-step:

  1. Read the file letter and the rank digit from the coordinate.
  2. Turn the file into a number, with a as 1 and h as 8.
  3. Turn the rank character into a number from 1 to 8.
  4. Add those two numbers.
  5. Return true when the sum is odd, which is white, and false when it is even.

Code Implementation

class Solution:
    def squareIsWhite(self, coordinates: str) -> bool:
        column_letter = coordinates[0]
        row_digit_string = coordinates[1]

        # Convert the column letter ('a'-'h') to a numerical value (1-8).
        # The ASCII value difference gives 0-7, so we add 1.
        column_number = ord(column_letter) - ord('a') + 1

        # Convert the row string character to its integer equivalent.
        row_number = int(row_digit_string)

        coordinate_sum = column_number + row_number

        # The color is determined by the parity of the sum of the coordinates.
        # If the sum is odd, the square is white (true); if even, it's black (false).
        return coordinate_sum % 2 != 0

Big(O) Analysis

Time Complexity
O(1) – The coordinate is always two characters. Converting the letter and the digit, adding them, and testing whether the sum is odd are each a fixed amount of work. Nothing loops over the board. The cost is the same for every square.
Space Complexity
O(1) – Only the file number, the rank number, and their sum are stored. No table of the 64 squares is built. Memory does not depend on which square was asked for. Extra space stays constant.

Edge Cases

The coordinate is a1
How to Handle:
That corner is black, so the result is false.
The coordinate is h8
How to Handle:
The opposite corner is also black, so the result is false.
The coordinate is a8
How to Handle:
That corner is white, so the result is true.
The coordinate is b1
How to Handle:
The square next to a1 has the opposite color, so the result is true.
The coordinate is a2
How to Handle:
Moving one rank flips the color, so the result is true.
0/86 completed