Skip to content

Commit

Permalink
Add problem 1876: Substrings of Size Three with Distinct Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 9, 2023
1 parent 05b5862 commit 1bdfbdb
Show file tree
Hide file tree
Showing 3 changed files with 55 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 @@ -1441,6 +1441,7 @@ pub mod problem_1860_incremental_memory_leak;
pub mod problem_1861_rotating_the_box;
pub mod problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternating;
pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros;
pub mod problem_1876_substrings_of_size_three_with_distinct_characters;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn count_good_substrings(s: String) -> i32 {
let mut result = 0;
let mut iter = s.bytes();
let mut first = iter.next().unwrap();
let mut second = first;

for c in iter {
result += i32::from(first != second && first != c && second != c);
first = second;
second = c;
}

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_good_substrings(s: String) -> i32 {
Self::count_good_substrings(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 count_good_substrings(s: String) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("xyzzaz", 1), ("aababcabc", 4)];

for (s, expected) in test_cases {
assert_eq!(S::count_good_substrings(s.to_string()), expected);
}
}
}

0 comments on commit 1bdfbdb

Please sign in to comment.