8000 Add 3274_check_if_two_chessboard_squares_have_the_same_color · m3xw3ll/LeetCode@f47dbde · GitHub
[go: up one dir, main page]

Skip to content

Commit f47dbde

Browse files
committed
Add 3274_check_if_two_chessboard_squares_have_the_same_color
1 parent ace2d96 commit f47dbde

3 files changed

+827
-776
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [Check if Two Chessboard Squares Have the Same Color](https://leetcode.com/problems/check-if-two-chessboard-squares-have-the-same-color/description/)
2+
3+
You are given two strings, coordinate1 and coordinate2, representing the coordinates of a square on an 8 x 8 chessboard.
4+
5+
Below is the chessboard for reference.
6+
7+
![](https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png)
8+
9+
Return true if these two squares have the same color and false otherwise.
10+
11+
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).
12+
13+
Example 1:
14+
```
15+
Input: coordinate1 = "a1", coordinate2 = "c3"
16+
17+
Output: true
18+
19+
Explanation:
20+
21+
Both squares are black.
22+
```
23+
Example 2:
24+
```
25+
Input: coordinate1 = "a1", coordinate2 = "h3"
26+
27+
Output: false
28+
29+
Explanation:
30+
31+
Square "a1" is black and "h3" is white.
32+
```
33+
Solution
34+
```python
35+
class Solution:
36+
def checkTwoChessboards(self, coordinate1: str, coordinate2: str) -> bool:
37+
c1_sum = int(ord(coordinate1[0].lower()) - ord('a') + 1) + int(coordinate1[1])
38+
c2_sum = int(ord(coordinate2[0].lower()) - ord('a') + 1) + int(coordinate2[1])
39+
return c1_sum % 2 == c2_sum % 2
40+
```
41+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def check_two_chessboards(coordinate1, coordinate2):
2+
c1_sum = int(ord(coordinate1[0].lower()) - ord('a') + 1) + int(coordinate1[1])
3+
c2_sum = int(ord(coordinate2[0].lower()) - ord('a') + 1) + int(coordinate2[1])
4+
return c1_sum % 2 == c2_sum % 2
5+
6+
7+
8+
9+
print(check_two_chessboards(coordinate1 = "a1", coordinate2 = "c3"))

0 commit comments

Comments
 (0)
0