Check if Two Chessboard Squares Have the Same Color - Problem
Chess Square Color Checker
Given two chessboard coordinates like
๐ Goal: Return
๐ Input: Two strings representing valid chessboard coordinates
โข First character: column letter ('a' to 'h')
โข Second character: row number ('1' to '8')
๐ค Output: Boolean indicating if squares have the same color
Fun fact: On a chessboard, square 'a1' is always a dark square, and colors alternate in a checkerboard pattern!
Given two chessboard coordinates like
"a1" and "h3", determine if they represent squares of the same color on a standard 8ร8 chessboard.๐ Goal: Return
true if both squares have the same color (both black or both white), false otherwise.๐ Input: Two strings representing valid chessboard coordinates
โข First character: column letter ('a' to 'h')
โข Second character: row number ('1' to '8')
๐ค Output: Boolean indicating if squares have the same color
Fun fact: On a chessboard, square 'a1' is always a dark square, and colors alternate in a checkerboard pattern!
Input & Output
example_1.py โ Basic Different Colors
$
Input:
coordinate1 = "a1", coordinate2 = "c3"
โบ
Output:
true
๐ก Note:
Square 'a1': column a(1) + row 1 = 2 (even) โ white. Square 'c3': column c(3) + row 3 = 6 (even) โ white. Both are white, so return true.
example_2.py โ Different Colors
$
Input:
coordinate1 = "a1", coordinate2 = "h3"
โบ
Output:
false
๐ก Note:
Square 'a1': column a(1) + row 1 = 2 (even) โ white. Square 'h3': column h(8) + row 3 = 11 (odd) โ black. Different colors, so return false.
example_3.py โ Same Square
$
Input:
coordinate1 = "a1", coordinate2 = "a1"
โบ
Output:
true
๐ก Note:
Both coordinates refer to the same square 'a1', so they obviously have the same color. Return true.
Constraints
- coordinate1.length == coordinate2.length == 2
- First character is a lowercase letter from 'a' to 'h'
- Second character is a digit from '1' to '8'
- Coordinates always represent valid chessboard squares
Visualization
Tap to expand
Understanding the Visualization
1
Coordinate System
Columns a-h map to 1-8, rows are already 1-8
2
Sum Calculation
Add column number and row number for each square
3
Color Rule
Even sum = white square, odd sum = black square
4
Comparison
Same parity means same color
Key Takeaway
๐ฏ Key Insight: Chessboard colors follow a simple mathematical pattern - the sum of coordinate numbers determines the color, making this an O(1) problem!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code