From b25995b2f45c6709b2c7cd5ab520fa91ccfd6cd8 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Wed, 10 Jan 2024 18:41:08 +0800 Subject: [PATCH] Add problem 1813: Sentence Similarity III --- src/lib.rs | 1 + .../iterative.rs | 59 +++++++++++++++++++ .../mod.rs | 36 +++++++++++ 3 files changed, 96 insertions(+) create mode 100644 src/problem_1813_sentence_similarity_iii/iterative.rs create mode 100644 src/problem_1813_sentence_similarity_iii/mod.rs diff --git a/src/lib.rs b/src/lib.rs index ad3963b1..e6e2e4b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1330,6 +1330,7 @@ pub mod problem_1802_maximum_value_at_a_given_index_in_a_bounded_array; pub mod problem_1805_number_of_different_integers_in_a_string; pub mod problem_1807_evaluate_the_bracket_pairs_of_a_string; pub mod problem_1812_determine_color_of_a_chessboard_square; +pub mod problem_1813_sentence_similarity_iii; pub mod problem_1814_count_nice_pairs_in_an_array; pub mod problem_1816_truncate_sentence; pub mod problem_1817_finding_the_users_active_minutes; diff --git a/src/problem_1813_sentence_similarity_iii/iterative.rs b/src/problem_1813_sentence_similarity_iii/iterative.rs new file mode 100644 index 00000000..c2efebbe --- /dev/null +++ b/src/problem_1813_sentence_similarity_iii/iterative.rs @@ -0,0 +1,59 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn check(source: &str, target: &str) -> bool { + let source = source.as_bytes(); + let target = target.as_bytes(); + let mut word_start = 0; + let mut iter = (1..).zip(source.iter().zip(target)); + + loop { + if let Some((i, (&lhs, &rhs))) = iter.next() { + if lhs != rhs { + break; + } + + if lhs == b' ' { + word_start = i; + } + } else { + if target.get(source.len()).map_or(true, |&c| c == b' ') { + return true; + } + + break; + } + } + + let split = target.len() - (source.len() - word_start); + + target.get(split.wrapping_sub(1)).map_or(true, |&c| c == b' ') && source[word_start..] == target[split..] + } + + pub fn are_sentences_similar(sentence1: String, sentence2: String) -> bool { + if sentence2.len() < sentence1.len() { + Self::check(&sentence2, &sentence1) + } else { + Self::check(&sentence1, &sentence2) + || (sentence1.len() == sentence2.len() && Self::check(&sentence2, &sentence1)) + } + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn are_sentences_similar(sentence1: String, sentence2: String) -> bool { + Self::are_sentences_similar(sentence1, sentence2) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1813_sentence_similarity_iii/mod.rs b/src/problem_1813_sentence_similarity_iii/mod.rs new file mode 100644 index 00000000..0c28062f --- /dev/null +++ b/src/problem_1813_sentence_similarity_iii/mod.rs @@ -0,0 +1,36 @@ +pub mod iterative; + +pub trait Solution { + fn are_sentences_similar(sentence1: String, sentence2: String) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (("My name is Haley", "My Haley"), true), + (("of", "A lot of words"), false), + (("Eating right now", "Eating"), true), + ( + ( + "B", + "ByI BMyQIqce b bARkkMaABi vlR RLHhqjNzCN oXvyK zRXR q ff B yHS OD KkvJA P JdWksnH", + ), + false, + ), + (("C", "CB B C"), true), + (("aa aAa", "aaA aAa"), false), + (("A A", "A aA"), false), + (("Are You Okay", "are you okay"), false), + ]; + + for ((sentence1, sentence2), expected) in test_cases { + assert_eq!( + S::are_sentences_similar(sentence1.to_string(), sentence2.to_string()), + expected, + ); + } + } +}