diff --git a/src/lib.rs b/src/lib.rs index 3391069c..209157d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1434,6 +1434,7 @@ pub mod problem_2090_k_radius_subarray_averages; pub mod problem_2091_removing_minimum_and_maximum_from_array; pub mod problem_2095_delete_the_middle_node_of_a_linked_list; pub mod problem_2099_find_subsequence_of_length_k_with_the_largest_sum; +pub mod problem_2103_rings_and_rods; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2103_rings_and_rods/iterative.rs b/src/problem_2103_rings_and_rods/iterative.rs new file mode 100644 index 00000000..6cf483c9 --- /dev/null +++ b/src/problem_2103_rings_and_rods/iterative.rs @@ -0,0 +1,40 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + #[allow(clippy::naive_bytecount)] // Not supported by LeetCode. + pub fn count_points(rings: String) -> i32 { + let mut iter_2 = rings.bytes(); + + iter_2.next(); + + let mut states = [0_u8; 10]; + + for (color, pole) in rings.bytes().zip(iter_2).step_by(2) { + states[usize::from(pole) - usize::from(b'0')] |= match color { + b'B' => 1, + b'G' => 2, + _ => 4, + }; + } + + states.iter().filter(|&&x| x == 7).count() as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn count_points(rings: String) -> i32 { + Self::count_points(rings) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2103_rings_and_rods/mod.rs b/src/problem_2103_rings_and_rods/mod.rs new file mode 100644 index 00000000..73efda62 --- /dev/null +++ b/src/problem_2103_rings_and_rods/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn count_points(rings: String) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("B0B6G0R6R0R6G9", 1), ("B0R0G0R9R0B0G0", 1), ("G4", 0)]; + + for (rings, expected) in test_cases { + assert_eq!(S::count_points(rings.to_string()), expected); + } + } +}