[LeetCode] Check if Two Chessboard Squares Have the Same Color

3274. Check if Two Chessboard Squares Have the Same Color

You are given two strings, coordinate1 and coordinate2, representing the coordinates of a square on an 8 x 8 chessboard.

Below is the chessboard for reference.

img

Return true if these two squares have the same color and false otherwise.

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public boolean isBlack(String field) {
char[] digs = field.toCharArray();
return (isOddColumn(digs[0]) && isOddRow(digs[1])) || (isEvenColumn(digs[0]) && isEvenRow(digs[1]));
}

public boolean isWhite(String field) {
return !isBlack(field);
}

private boolean isOddColumn(char col) {
return col == 'a' || col == 'c' || col == 'e' || col == 'g';
}

private boolean isEvenColumn(char col) {
return col == 'b' || col == 'd' || col == 'f' || col == 'h';
}

private boolean isOddRow(char row) {
return row == '1' || row == '3' || row == '5' || row == '7';
}

private boolean isEvenRow(char row) {
return row == '2' || row == '4' || row == '6' || row == '8';
}
public boolean checkTwoChessboards(String coordinate1, String coordinate2) {
return this.isBlack(coordinate1) == this.isBlack(coordinate2);
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/01/PS/LeetCode/check-if-two-chessboard-squares-have-the-same-color/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.