Skip to content

Commit 2ae2e39

Browse files
authored
1812. Determine Color of a Chessboard Square
1 parent 7306c29 commit 2ae2e39

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

String/squareIsWhite.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
```
2+
1812. Determine Color of a Chessboard Square
3+
4+
You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.
5+
6+
Return true if the square is white, and false if the square is black.
7+
8+
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.
9+
10+
11+
12+
Example 1:
13+
14+
Input: coordinates = "a1"
15+
Output: false
16+
Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.
17+
18+
Example 2:
19+
20+
Input: coordinates = "h3"
21+
Output: true
22+
Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.
23+
24+
Example 3:
25+
26+
Input: coordinates = "c7"
27+
Output: false
28+
29+
```
30+
class Solution {
31+
public:
32+
bool squareIsWhite(string coordinates) {
33+
int col = coordinates[0]-'a' + 1;
34+
int row = coordinates[1];
35+
36+
return (col+row)%2 == 1;
37+
}
38+
};

0 commit comments

Comments
 (0)