-
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 2108: Find First Palindromic String in the Array
- Loading branch information
Showing
3 changed files
with
68 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
42 changes: 42 additions & 0 deletions
42
src/problem_2108_find_first_palindromic_string_in_the_array/iterative.rs
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,42 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn first_palindrome(words: Vec<String>) -> String { | ||
words | ||
.into_iter() | ||
.find(|word| { | ||
let mut iter = word.bytes(); | ||
|
||
while let Some(lhs) = iter.next() { | ||
if let Some(rhs) = iter.next_back() { | ||
if lhs != rhs { | ||
return false; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
true | ||
}) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn first_palindrome(words: Vec<String>) -> String { | ||
Self::first_palindrome(words) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/problem_2108_find_first_palindromic_string_in_the_array/mod.rs
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,25 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn first_palindrome(words: Vec<String>) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&["abc", "car", "ada", "racecar", "cool"] as &[_], "ada"), | ||
(&["notapalindrome", "racecar"], "racecar"), | ||
(&["def", "ghi"], ""), | ||
]; | ||
|
||
for (words, expected) in test_cases { | ||
assert_eq!( | ||
S::first_palindrome(words.iter().copied().map(str::to_string).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |