LeetCode-in-Java

3274. Check if Two Chessboard Squares Have the Same Color

Easy

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.

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

Example 1:

Input: coordinate1 = “a1”, coordinate2 = “c3”

Output: true

Explanation:

Both squares are black.

Example 2:

Input: coordinate1 = “a1”, coordinate2 = “h3”

Output: false

Explanation:

Square "a1" is black and "h3" is white.

Constraints:

Solution

public class Solution {
    public boolean checkTwoChessboards(String c1, String c2) {
        int s1 = (c1.charAt(0) - 'a') + (c1.charAt(1) - '0');
        int s2 = (c2.charAt(0) - 'a') + (c2.charAt(1) - '0');
        return s1 % 2 == s2 % 2;
    }
}