LeetCode Problem Workspace
Determine Color of a Chessboard Square
Determine whether a given chessboard square is white or black by converting coordinates using a math plus string approach.
2
Topics
8
Code langs
3
Related
Practice Focus
Easy · Math plus String
Answer-first summary
Determine whether a given chessboard square is white or black by converting coordinates using a math plus string approach.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus String
This problem asks you to identify the color of a chessboard square based on its coordinates. You can solve it by converting the letter and number into numerical indices and using arithmetic to determine parity. The solution leverages a math plus string pattern, ensuring fast evaluation and minimal computation for any valid input.
Problem Statement
You are given a string representing the coordinates of a square on a standard 8x8 chessboard, such as "a1" or "h8". Determine whether the square is white or black by analyzing its position.
Return true if the square is white and false if it is black. The input will always be a valid chessboard coordinate with a letter first (from 'a' to 'h') and a number second (from '1' to '8').
Examples
Example 1
Input: coordinates = "a1"
Output: false
From the chessboard above, the square with coordinates "a1" is black, so return false.
Example 2
Input: coordinates = "h3"
Output: true
From the chessboard above, the square with coordinates "h3" is white, so return true.
Example 3
Input: coordinates = "c7"
Output: false
Example details omitted.
Constraints
- coordinates.length == 2
- 'a' <= coordinates[0] <= 'h'
- '1' <= coordinates[1] <= '8'
Solution Approach
Convert letter to numeric index
Map the first character of the coordinate to a number from 1 to 8 (e.g., 'a' → 1, 'b' → 2) to align with column indices. This simplifies later parity checks.
Check row parity
Convert the second character (number) directly into an integer representing the row. Combine it with the column index to calculate whether the sum is even or odd.
Determine color using sum parity
Add the numeric column and row indices. If the sum is even, the square is black; if odd, the square is white. Return the boolean result accordingly.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because coordinate conversion and arithmetic are constant operations. Space complexity is O(1) as only a few integer variables are needed.
What Interviewers Usually Probe
- The candidate understands converting characters to numeric indices for computation.
- They identify that parity determines the chessboard color pattern.
- They correctly handle the mapping of letters 'a' to 'h' to numerical values without off-by-one errors.
Common Pitfalls or Variants
Common pitfalls
- Misinterpreting the color mapping by reversing black and white parity.
- Off-by-one errors when converting letters to numbers ('a' vs 1).
- Ignoring that the sum of indices, not individual indices, determines the color.
Follow-up variants
- Determine color for a rectangular chessboard of arbitrary size using the same parity approach.
- Return the color as a string 'white' or 'black' instead of a boolean.
- Given multiple coordinates, compute an array of boolean results for all squares efficiently.
FAQ
How do I convert a chessboard letter to a number in this problem?
Map letters 'a' to 'h' to numbers 1 through 8, which allows you to compute sums for parity checking.
Why does adding row and column indices determine the square color?
Because the chessboard alternates colors, the sum of column and row indices being even or odd directly maps to black or white squares.
Can this approach handle multiple coordinates at once?
Yes, convert each coordinate individually and apply the sum parity logic to compute a boolean array of results.
What pattern does this problem illustrate?
This is a Math plus String pattern where string parsing is combined with numeric calculations to evaluate parity.
What are common mistakes when implementing this solution?
Reversing the color logic, off-by-one errors in letter-to-number conversion, or summing indices incorrectly.
Solution
Solution 1: Pattern Recognition
Observing the chessboard, we find that two squares $(x_1, y_1)$ and $(x_2, y_2)$ with the same color satisfy that both $x_1 + y_1$ and $x_2 + y_2$ are either odd or even.
class Solution:
def squareIsWhite(self, coordinates: str) -> bool:
return (ord(coordinates[0]) + ord(coordinates[1])) % 2 == 1Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward