-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1813: Sentence Similarity III
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} |