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

Valid Number Problem

LeetCode 65. Given a string s, return true if s...

Text Justification Problem

LeetCode 68. Given an array of strings words and a...

Valid Number Problem

LeetCode 67. A robot is located at the top-left corner...

Length Of Last Word Problem

LeetCode 58. Given a string s consisting of some words...

Zigzag Conversion Problem

LeetCode 6. Write the code that will take a string...

Wildcard Matching Problem

LeetCode 44. Write the code that will take a string...