Skip to content

Commit 15a9582

Browse files
committed
Add problem 3127: Make a Square with the Same Color
1 parent 62f1757 commit 15a9582

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,7 @@ pub mod problem_3114_latest_time_you_can_obtain_after_replacing_characters;
20972097
pub mod problem_3115_maximum_prime_difference;
20982098
pub mod problem_3120_count_the_number_of_special_characters_i;
20992099
pub mod problem_3121_count_the_number_of_special_characters_ii;
2100+
pub mod problem_3127_make_a_square_with_the_same_color;
21002101
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21012102

21022103
#[cfg(test)]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn can_make_square(grid: Vec<Vec<char>>) -> bool {
7+
let [[m00, m01, m02], [m10, m11, m12], [m20, m21, m22]] = <[_; 3]>::try_from(grid)
8+
.ok()
9+
.unwrap()
10+
.map(|row| <[_; 3]>::try_from(row).ok().unwrap());
11+
12+
[
13+
[m00, m01, m10, m11],
14+
[m01, m02, m11, m12],
15+
[m10, m11, m20, m21],
16+
[m11, m12, m21, m22],
17+
]
18+
.iter()
19+
.any(|values| values.iter().filter(|&&c| c == 'B').count() != 2)
20+
}
21+
}
22+
23+
// ------------------------------------------------------ snip ------------------------------------------------------ //
24+
25+
impl super::Solution for Solution {
26+
fn can_make_square(grid: Vec<Vec<char>>) -> bool {
27+
Self::can_make_square(grid)
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
#[test]
34+
fn test_solution() {
35+
super::super::tests::run::<super::Solution>();
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub mod brute_force;
2+
3+
pub trait Solution {
4+
fn can_make_square(grid: Vec<Vec<char>>) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(["BWB", "BWW", "BWB"], true),
14+
(["BWB", "WBW", "BWB"], false),
15+
(["BWB", "BWW", "BWW"], true),
16+
(["BBB", "BBB", "BBB"], true),
17+
];
18+
19+
for (board, expected) in test_cases {
20+
assert_eq!(
21+
S::can_make_square(board.iter().map(|row| row.chars().collect()).collect()),
22+
expected,
23+
);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)