Skip to content

Commit

Permalink
Add problem 1813: Sentence Similarity III
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 10, 2024
1 parent f334e49 commit b25995b
Show file tree
Hide file tree
Showing 3 changed files with 96 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 @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions src/problem_1813_sentence_similarity_iii/iterative.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
36 changes: 36 additions & 0 deletions src/problem_1813_sentence_similarity_iii/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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,
);
}
}
}

0 comments on commit b25995b

Please sign in to comment.