Skip to content

Commit

Permalink
1812. Determine Color of a Chessboard Square: AC
Browse files Browse the repository at this point in the history
  • Loading branch information
tan-wei committed Dec 20, 2024
1 parent d0e6a42 commit d47ccd3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/solution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,4 @@ mod s1805_number_of_different_integers_in_a_string;
mod s1806_minimum_number_of_operations_to_reinitialize_a_permutation;
mod s1807_evaluate_the_bracket_pairs_of_a_string;
mod s1808_maximize_number_of_nice_divisors;
mod s1812_determine_color_of_a_chessboard_square;
84 changes: 84 additions & 0 deletions src/solution/s1812_determine_color_of_a_chessboard_square.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* [1812] 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.
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" />
* 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'
*
*/
pub struct Solution {}

// problem: https://leetcode.com/problems/determine-color-of-a-chessboard-square/
// discuss: https://leetcode.com/problems/determine-color-of-a-chessboard-square/discuss/?currentPage=1&orderBy=most_votes&query=

// submission codes start here

impl Solution {
pub fn square_is_white(coordinates: String) -> bool {
coordinates
.as_bytes()
.into_iter()
.map(|c| c % 2)
.sum::<u8>()
== 1
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_1812_example_1() {
let coordinates = "a1".to_string();

let result = false;

assert_eq!(Solution::square_is_white(coordinates), result);
}

#[test]
fn test_1812_example_2() {
let coordinates = "h3".to_string();

let result = true;

assert_eq!(Solution::square_is_white(coordinates), result);
}

#[test]
fn test_1812_example_3() {
let coordinates = "c7".to_string();

let result = false;

assert_eq!(Solution::square_is_white(coordinates), result);
}
}

0 comments on commit d47ccd3

Please sign in to comment.