3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution

In this guide, you will get 3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution with the best time and space complexity. The solution to Check if Two Chessboard Squares Have the Same Color problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Two Chessboard Squares Have the Same Color solution in C++
  4. Check if Two Chessboard Squares Have the Same Color solution in Java
  5. Check if Two Chessboard Squares Have the Same Color solution in Python
  6. Additional Resources
3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution image

Problem Statement of 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.

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:

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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)
See also  162. Find Peak ElementLeetCode Solution

3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution in C++

class Solution {
 public:
  bool checkTwoChessboards(string coordinate1, string coordinate2) {
    return squareIsWhite(coordinate1) == squareIsWhite(coordinate2);
  }

 private:
  // Same as 1812. Determine Color of a Chessboard Square
  bool squareIsWhite(const string& coordinates) {
    const char letter = coordinates[0];
    const char digit = coordinates[1];
    return letter % 2 != digit % 2;
  }
};
/* code provided by PROGIEZ */

3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution in Java

class Solution {
  public boolean checkTwoChessboards(String coordinate1, String coordinate2) {
    return squareIsWhite(coordinate1) == squareIsWhite(coordinate2);
  }

  // Same as 1812. Determine Color of a Chessboard Square
  private boolean squareIsWhite(final String coordinates) {
    final char letter = coordinates.charAt(0);
    final char digit = coordinates.charAt(1);
    return letter % 2 != digit % 2;
  }
}
// code provided by PROGIEZ

3274. Check if Two Chessboard Squares Have the Same Color LeetCode Solution in Python

class Solution:
  def checkTwoChessboards(self, coordinate1: str, coordinate2: str) -> bool:
    # Same as 1812. Determine Color of a Chessboard Square
    def squareIsWhite(coordinate: str) -> bool:
      letter, digit = coordinate
      return ord(letter) % 2 != int(digit) % 2

    return squareIsWhite(coordinate1) == squareIsWhite(coordinate2)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.