Problem

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

Examples

Example 1

1
2
3
4
Input: coordinate1 = "a1", coordinate2 = "c3"
Output: true
Explanation:
Both squares are black.

Example 2

1
2
3
4
Input: coordinate1 = "a1", coordinate2 = "h3"
Output: false
Explanation:
Square `"a1"` is black and `"h3"` is white.

Constraints

  • coordinate1.length == coordinate2.length == 2
  • 'a' <= coordinate1[0], coordinate2[0] <= 'h'
  • '1' <= coordinate1[1], coordinate2[1] <= '8'

Solution

Method 1 – Parity Calculation

Intuition

On a chessboard, the color of a square is determined by the parity of the sum of its row and column indices. If two squares have the same parity, they have the same color.

Reasoning

Columns are labeled ‘a’ to ‘h’ (1 to 8) and rows ‘1’ to ‘8’. For a square, if (column index + row index) is even, it’s black; if odd, it’s white. Thus, two squares have the same color if their parities match.

Approach

  1. Convert the column letter to a number (a=1, b=2, …, h=8).
  2. Convert the row character to a number (1-8).
  3. For both coordinates, compute the sum of column and row indices and check their parity.
  4. Return true if both parities match, otherwise false.

Edge cases:

  • Both coordinates are the same.

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
    bool squareIsSameColor(string c1, string c2) {
        auto get = [](const string& c) {
            return ((c[0] - 'a' + 1) + (c[1] - '0')) % 2;
        };
        return get(c1) == get(c2);
    }
};
1
2
3
4
5
6
func squareIsSameColor(c1, c2 string) bool {
    get := func(c string) int {
        return (int(c[0]-'a'+1) + int(c[1]-'0')) % 2
    }
    return get(c1) == get(c2)
}
1
2
3
4
5
6
class Solution {
    public boolean squareIsSameColor(String c1, String c2) {
        return ((c1.charAt(0) - 'a' + 1 + c1.charAt(1) - '0') % 2) ==
               ((c2.charAt(0) - 'a' + 1 + c2.charAt(1) - '0') % 2);
    }
}
1
2
3
4
5
6
class Solution {
    fun squareIsSameColor(c1: String, c2: String): Boolean {
        fun get(c: String) = (c[0] - 'a' + 1 + c[1] - '0') % 2
        return get(c1) == get(c2)
    }
}
1
2
3
4
5
class Solution:
    def square_is_same_color(self, c1: str, c2: str) -> bool:
        def get(c: str) -> int:
            return (ord(c[0]) - ord('a') + 1 + int(c[1])) % 2
        return get(c1) == get(c2)
1
2
3
4
5
6
7
8
9
impl Solution {
    pub fn square_is_same_color(c1: String, c2: String) -> bool {
        fn get(c: &str) -> i32 {
            let b = c.as_bytes();
            ((b[0] - b'a' + 1) as i32 + (b[1] - b'0') as i32) % 2
        }
        get(&c1) == get(&c2)
    }
}

Complexity

  • ⏰ Time complexity: O(1), as all operations are constant time.
  • 🧺 Space complexity: O(1), as only a few variables are used.