-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1869: Substrings of Size Three with Distinct Characters
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1869_longer_contiguous_segments_of_ones_than_zeros/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |