rs: add «2108. Find First Palindromic String in the Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
6fa8544de7
commit
0ed6d6ed64
1 changed files with 14 additions and 0 deletions
14
rs/find-first-palindromic-string-in-the-array.rs
Normal file
14
rs/find-first-palindromic-string-in-the-array.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
impl Solution {
|
||||
fn is_palindrome(word: &str) -> bool {
|
||||
word.chars()
|
||||
.take(word.len() >> 1)
|
||||
.eq(word.chars().rev().take(word.len() >> 1))
|
||||
}
|
||||
|
||||
pub fn first_palindrome(words: Vec<String>) -> String {
|
||||
words
|
||||
.into_iter()
|
||||
.find(|s| Self::is_palindrome(s))
|
||||
.unwrap_or(String::from(""))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue