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'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:
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:
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_squareTurn 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:
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| Case | How to Handle |
|---|---|
| The coordinate is a1 | That corner is black, so the result is false. |
| The coordinate is h8 | The opposite corner is also black, so the result is false. |
| The coordinate is a8 | That corner is white, so the result is true. |
| The coordinate is b1 | The square next to a1 has the opposite color, so the result is true. |
| The coordinate is a2 | Moving one rank flips the color, so the result is true. |