You are given a string coordinates
that represents the coordinates of a square of the chessboard. Below are some points for better understanding:
"<letter><digit>"
, where <letter>
is a letter from a
to h
representing the column and <digit>
is a digit from 1
to 8
representing the row of the chessboard.Your task is to determine whether the square is white or black.
Return true
if the square is white, and false
if the square is black.
A chessboard is colored in an alternating pattern. Specifically:
a1
is black.false
if the square is black.true
if the square is white.The strategy to solve this problem involves:
The pattern can be summarized as:
def squareIsWhite(coordinates: str) -> bool:
column = coordinates[0]
row = coordinates[1]
# Convert column from a-h to 1-8
column_number = ord(column) - ord('a') + 1
row_number = int(row)
# Check the parity
return (column_number + row_number) % 2 != 0
# Example Usage:
coordinates = "a1"
print(squareIsWhite(coordinates)) # Output: False (Black square)
The time complexity of this solution is O(1) since we perform a fixed number of operations regardless of the input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?