From 72deb05de11b9820bea660e22f5a67df0dff577a Mon Sep 17 00:00:00 2001 From: EFanZh Date: Thu, 28 Sep 2023 19:48:05 +0800 Subject: [PATCH] Add problem 1869: Substrings of Size Three with Distinct Characters --- src/lib.rs | 1 + .../iterative.rs | 64 +++++++++++++++++++ .../mod.rs | 18 ++++++ 3 files changed, 83 insertions(+) create mode 100644 src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/iterative.rs create mode 100644 src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 0d20f0f8..c741c06e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1429,6 +1429,7 @@ pub mod problem_1854_maximum_population_year; pub mod problem_1855_maximum_distance_between_a_pair_of_values; pub mod problem_1859_sorting_the_sentence; pub mod problem_1860_incremental_memory_leak; +pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros; #[cfg(test)] mod test_utilities; diff --git a/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/iterative.rs b/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/iterative.rs new file mode 100644 index 00000000..3cf5474c --- /dev/null +++ b/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/iterative.rs @@ -0,0 +1,64 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn check_zero_ones(s: String) -> bool { + let mut iter = s.bytes(); + let mut longest_zeros = 0_u8; + let mut longest_ones = 0_u8; + let mut length = 0_u8; + + 'outer: loop { + if let Some(c) = iter.next() { + if c == b'0' { + length += 1; + } else { + longest_zeros = longest_zeros.max(length); + + length = 1; + + loop { + if let Some(c) = iter.next() { + if c == b'0' { + longest_ones = longest_ones.max(length); + + length = 1; + + break; + } + + length += 1; + } else { + longest_ones = longest_ones.max(length); + + break 'outer; + } + } + } + } else { + longest_zeros = longest_zeros.max(length); + + break; + } + } + + longest_ones > longest_zeros + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn check_zero_ones(s: String) -> bool { + Self::check_zero_ones(s) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/mod.rs b/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/mod.rs new file mode 100644 index 00000000..30a5de72 --- /dev/null +++ b/src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn check_zero_ones(s: String) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("1101", true), ("111000", false), ("110100010", false)]; + + for (s, expected) in test_cases { + assert_eq!(S::check_zero_ones(s.to_string()), expected); + } + } +}