Skip to content

Commit

Permalink
Add problem 1869: Substrings of Size Three with Distinct Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 28, 2023
1 parent d1e99c3 commit 72deb05
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit 72deb05

Please sign in to comment.