Problem Description
Given two chessboard square coordinates (like "a1" or "h3") from an 8x8 chessboard, determine if they are of the same color. The chessboard alternates colors, and the coordinate consists of a letter (column) and a digit (row).
Key Insights
- Each square's color can be determined by the parity (odd/even) of the sum of its column index and row number.
- Column letters ('a' to 'h') are mapped to numbers (1 to 8). Adding this to the row digit gives a sum; even sums correspond to one color, odd sums to the opposite.
- No additional data structures are required; only arithmetic operations are used.
Space and Time Complexity
Time Complexity: O(1) - Only constant time arithmetic operations are performed. Space Complexity: O(1) - Only a few integer variables are used.
Solution
We convert the column, represented by a letter, to a numeric index by subtracting the ASCII value of 'a' and adding 1. We convert the row character to an integer. By summing these values and taking the result modulo 2, we can determine the square's color. Two squares have the same color if their corresponding modulo results are equal. This approach efficiently checks the parity of the sum for both coordinates.