From 0ed6d6ed647ac034c6fa57d421ca1b32a0d503fc Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 13 Feb 2024 11:10:47 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB2108.=20Find=20First=20Palind?= =?UTF-8?q?romic=20String=20in=20the=20Array=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/find-first-palindromic-string-in-the-array.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 rs/find-first-palindromic-string-in-the-array.rs diff --git a/rs/find-first-palindromic-string-in-the-array.rs b/rs/find-first-palindromic-string-in-the-array.rs new file mode 100644 index 0000000..2dd7bb9 --- /dev/null +++ b/rs/find-first-palindromic-string-in-the-array.rs @@ -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 { + words + .into_iter() + .find(|s| Self::is_palindrome(s)) + .unwrap_or(String::from("")) + } +}