Determine Color Of A Chessboard Square Problem


Description

LeetCode Problem 1812.

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

1
2
3
4
5
6
7
8
9
8 w b w b w b w b 
7 b w b w b w b w
6 w b w b w b w b 
5 b w b w b w b w
4 w b w b w b w b 
3 b w b w b w b w
2 w b w b w b w b 
1 b w b w b w b w
  a b c d e f g h

(Note: “b” stands for black square, and “w” stands for white square.)

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:

1
2
3
Input: coordinates = "a1"
Output: false
Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.

Example 2:

1
2
3
Input: coordinates = "h3"
Output: true
Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.

Example 3:

1
2
Input: coordinates = "c7"
Output: false

Constraints:

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


Sample C++ Code

This is a C++ solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    bool squareIsWhite(string coordinates) {
        char t = coordinates[0];
        char s = coordinates[1];
        if(t == 'a' || t == 'c' || t == 'e' || t == 'g')
        {
            if((s - '0') % 2 == 1) return false;
            else return true;    
        }
        else
        {
            if((s - '0') % 2 == 0) return false;
            else return true;
        }
    }
};




Related Posts

String Without Aaa Or Bbb Problem

LeetCode 984. Given two integers a and b, return any...

Shifting Letters Problem

LeetCode 848. You are given a string s of lowercase...

Positions Of Large Groups Problem

LeetCode 830. In a string sof lowercase letters, these letters...

Orderly Queue Problem

LeetCode 899. You are given a string s and an...

Number Of Lines To Write String Problem

LeetCode 806. You are given a string s of lowercase...

Masking Personal Information Problem

LeetCode 831. You are given a personal information string s,...