From 5c67d0c4ea8238db0d7f19f8198fdd0daed0bcfd Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 18 Nov 2023 21:17:46 +0800 Subject: [PATCH] Add problem 1967: Number of Strings That Appear as Substrings in Word --- src/lib.rs | 1 + .../brute_force.rs | 24 ++++++++++++++++++ .../mod.rs | 25 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/brute_force.rs create mode 100644 src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 37e3b217..805146df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/brute_force.rs b/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/brute_force.rs new file mode 100644 index 00000000..78efcd02 --- /dev/null +++ b/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/brute_force.rs @@ -0,0 +1,24 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // +impl Solution { + pub fn num_of_strings(patterns: Vec, 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, word: String) -> i32 { + Self::num_of_strings(patterns, word) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/mod.rs b/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/mod.rs new file mode 100644 index 00000000..078bd7c4 --- /dev/null +++ b/src/problem_1967_number_of_strings_that_appear_as_substrings_in_word/mod.rs @@ -0,0 +1,25 @@ +pub mod brute_force; + +pub trait Solution { + fn num_of_strings(patterns: Vec, word: String) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + 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, + ); + } + } +}