Skip to content

Commit

Permalink
Add problem 1967: Number of Strings That Appear as Substrings in Word
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 18, 2023
1 parent 813e3d8 commit 5c67d0c
Show file tree
Hide file tree
Showing 3 changed files with 50 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 @@ -1481,6 +1481,7 @@ pub mod problem_1952_three_divisors;
pub mod problem_1957_delete_characters_to_make_fancy_string;
pub mod problem_1961_check_if_string_is_a_prefix_of_array;
pub mod problem_1962_remove_stones_to_minimize_the_total;
pub mod problem_1967_number_of_strings_that_appear_as_substrings_in_word;

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

// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
patterns.into_iter().filter(|p| word.contains(p)).count() as _
}
}

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

impl super::Solution for Solution {
fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
Self::num_of_strings(patterns, word)
}
}

#[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,25 @@
pub mod brute_force;

pub trait Solution {
fn num_of_strings(patterns: Vec<String>, word: String) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [
((&["a", "abc", "bc", "d"] as &[_], "abc"), 3),
((&["a", "b", "c"], "aaaaabbbbb"), 2),
((&["a", "a", "a"], "ab"), 3),
];

for ((patterns, word), expected) in test_cases {
assert_eq!(
S::num_of_strings(patterns.iter().copied().map(str::to_string).collect(), word.to_string()),
expected,
);
}
}
}

0 comments on commit 5c67d0c

Please sign in to comment.