1812. Determine Color of a Chessboard Square LeetCode Solution

In this guide, you will get 1812. Determine Color of a Chessboard Square LeetCode Solution with the best time and space complexity. The solution to Determine Color of a Chessboard Square 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. Determine Color of a Chessboard Square solution in C++
  4. Determine Color of a Chessboard Square solution in Java
  5. Determine Color of a Chessboard Square solution in Python
  6. Additional Resources
1812. Determine Color of a Chessboard Square LeetCode Solution image

Problem Statement of Determine Color of a Chessboard Square

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

Example 1:

Input: coordinates = “a1”
Output: false
Explanation: From the chessboard above, the square with coordinates “a1” is black, so return false.

Example 2:

Input: coordinates = “h3”
Output: true
Explanation: From the chessboard above, the square with coordinates “h3” is white, so return true.

Example 3:

Input: coordinates = “c7”
Output: false

Constraints:

coordinates.length == 2
‘a’ <= coordinates[0] <= 'h'
'1' <= coordinates[1] <= '8'

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

1812. Determine Color of a Chessboard Square LeetCode Solution in C++

class Solution {
 public:
  bool squareIsWhite(string coordinates) {
    const char letter = coordinates[0];
    const char digit = coordinates[1];
    return letter % 2 != digit % 2;
  }
};
/* code provided by PROGIEZ */

1812. Determine Color of a Chessboard Square LeetCode Solution in Java

class Solution {
  public boolean squareIsWhite(String coordinates) {
    final char letter = coordinates.charAt(0);
    final char digit = coordinates.charAt(1);
    return letter % 2 != digit % 2;
  }
}
// code provided by PROGIEZ

1812. Determine Color of a Chessboard Square LeetCode Solution in Python

class Solution:
  def squareIsWhite(self, coordinates: str) -> bool:
    letter, digit = coordinates
    return ord(letter) % 2 != int(digit) % 2
# code by PROGIEZ

Additional Resources

See also  371. Sum of Two Integers LeetCode Solution

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