Difficulty: Easy | Acceptance: 79.80% | Paid: No Topics: Math, String
You are given coordinates, a string that represents the position of a square on the chessboard.
The chessboard is an 8x8 grid, where the squares are colored alternately white and black. The top-left corner (a1) is black.
Return true if the square is white, and false otherwise.
The coordinate string always represents a valid chessboard square.
- Examples
- Constraints
- Mathematical Parity Check
- ASCII Value Calculation
- Set Lookup
Examples
Example 1:
Input: coordinates = "a1"
Output: false
Explanation: "a1" is a black square.
Example 2:
Input: coordinates = "h3"
Output: true
Explanation: "h3" is a white square.
Example 3:
Input: coordinates = "c7"
Output: false
Constraints
coordinates.length == 2
'a' <= coordinates[0] <= 'h'
'1' <= coordinates[1] <= '8'
Mathematical Parity Check
Intuition The color of a square depends on the parity (even or odd nature) of the sum of its row and column indices. If the sum is even, the square is white; if odd, it is black.
Steps
- Extract the column letter (a-h) and convert it to a 0-based index (a=0, b=1, etc.).
- Extract the row number (1-8) and convert it to an integer.
- Calculate the sum of the column index and the row number.
- Return true if the sum is even, false otherwise.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
col = ord(coordinates[0]) - ord('a')
row = int(coordinates[1])
return (col + row) % 2 == 0Complexity
- Time: O(1)
- Space: O(1)
- Notes: This is the most optimal approach with constant time operations.
ASCII Value Calculation
Intuition We can leverage the ASCII values of the characters directly to perform the parity check without explicitly mapping the letter to an index variable, though the logic remains mathematically identical to the parity check.
Steps
- Calculate the difference between the ASCII value of the first character and ‘a’.
- Calculate the difference between the ASCII value of the second character and ‘0’.
- Sum these differences and check if the result is even.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
return (ord(coordinates[0]) + int(coordinates[1])) % 2 == 1Complexity
- Time: O(1)
- Space: O(1)
- Notes: This is a concise implementation relying on ASCII properties.
Set Lookup
Intuition Since the chessboard size is fixed and small (8x8), we can pre-compute all white squares and store them in a set. We then simply check if the input coordinate exists in that set.
Steps
- Initialize a set containing all coordinates of white squares.
- Check if the input
coordinatesstring is present in the set. - Return the result of the check.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
white_squares = {
"a2", "a4", "a6", "a8",
"b1", "b3", "b5", "b7",
"c2", "c4", "c6", "c8",
"d1", "d3", "d5", "d7",
"e2", "e4", "e6", "e8",
"f1", "f3", "f5", "f7",
"g2", "g4", "g6", "g8",
"h1", "h3", "h5", "h7"
}
return coordinates in white_squaresComplexity
- Time: O(1) (Lookup in a set is O(1) on average)
- Space: O(1) (The set size is fixed at 32 elements)
- Notes: Less efficient in terms of code size and memory compared to the mathematical approach, but conceptually simple.