LeetCode Problem Workspace
Check if Two Chessboard Squares Have the Same Color
Determine if two chessboard squares share the same color using coordinate math and simple string manipulation for quick evaluation.
2
Topics
5
Code langs
3
Related
Practice Focus
Easy · Math plus String
Answer-first summary
Determine if two chessboard squares share the same color using coordinate math and simple string manipulation for quick evaluation.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus String
To solve this problem, immediately map the letters of the coordinates to column numbers and sum them with the row numbers. If the sum of each square modulo 2 matches, the squares share the same color. This uses a combination of string parsing and simple arithmetic to directly determine black or white squares.
Problem Statement
You are given two strings, coordinate1 and coordinate2, each representing a square on a standard 8x8 chessboard with columns 'a' to 'h' and rows '1' to '8'. Your task is to decide whether both squares are the same color.
Return true if the squares share the same color and false if they differ. For example, coordinate1 = "a1" and coordinate2 = "c3" both point to black squares, so the answer is true. This problem combines string parsing with arithmetic logic for fast evaluation.
Examples
Example 1
Input: coordinate1 = "a1", coordinate2 = "c3"
Output: true
Both squares are black.
Example 2
Input: coordinate1 = "a1", coordinate2 = "h3"
Output: false
Square "a1" is black and "h3" is white.
Constraints
- coordinate1.length == coordinate2.length == 2
- 'a' <= coordinate1[0], coordinate2[0] <= 'h'
- '1' <= coordinate1[1], coordinate2[1] <= '8'
Solution Approach
Map Columns to Numbers
Convert the first character of each coordinate from 'a'-'h' to integers 1-8, then combine with the row number from the second character. This creates a numeric representation for each square.
Check Parity for Color
Sum the column and row numbers for each square and take modulo 2. Squares with the same modulo result share the same color. This directly applies the chessboard color pattern rule.
Return Boolean Result
Compare the parity results of both squares. Return true if they match and false otherwise. This gives an O(1) solution using simple arithmetic and string indexing.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The solution runs in constant time and uses constant space because it only processes two fixed-length strings and performs a few arithmetic operations without extra data structures.
What Interviewers Usually Probe
- Checks if you understand mapping characters to numbers for arithmetic checks.
- Tests ability to combine string parsing with modular arithmetic.
- Observes if you optimize for O(1) rather than iterating the board.
Common Pitfalls or Variants
Common pitfalls
- Confusing row and column indices when converting letters to numbers.
- Forgetting to apply modulo 2, leading to incorrect color comparisons.
- Assuming a fixed board starting color without verifying the coordinate sums.
Follow-up variants
- Determine if multiple pairs of squares all share the same color efficiently.
- Check the color difference for a variable-sized chessboard using the same parity logic.
- Return the color ('black' or 'white') instead of a boolean to generalize the solution.
FAQ
What does 'Check if Two Chessboard Squares Have the Same Color' mean in code terms?
It means converting each coordinate to numeric row and column, summing them, and comparing the parity to decide if the squares share the same color.
Can this approach handle any valid chessboard coordinates?
Yes, as long as the inputs are in the range 'a'-'h' for columns and '1'-'8' for rows, the parity method works reliably.
Why is modulo 2 used in this problem?
Because the color alternates every square, so the sum of row and column being even or odd directly determines black or white.
Is this solution efficient for multiple queries?
Yes, each check is O(1), so multiple coordinate pairs can be processed quickly without iterating the board.
What are common mistakes to avoid?
Mixing up row and column mapping, forgetting to convert letters to numbers, and skipping the modulo operation are frequent errors.
Solution
Solution 1: Mathematics
We calculate the differences in the x-coordinates and y-coordinates of the two points. If the sum of these differences is even, then the colors of the squares at these two coordinates are the same; otherwise, they are different.
class Solution:
def checkTwoChessboards(self, coordinate1: str, coordinate2: str) -> bool:
x = ord(coordinate1[0]) - ord(coordinate2[0])
y = int(coordinate1[1]) - int(coordinate2[1])
return (x + y) % 2 == 0Continue 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