rs: add «2108. Find First Palindromic String in the Array»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-02-13 11:10:47 +01:00
parent 6fa8544de7
commit 0ed6d6ed64
Signed by: mfocko
GPG key ID: 7C47D46246790496

View 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(""))
}
}