Created
April 9, 2024 14:55
-
-
Save preston-56/e38a0356fc85157a81cb14cba23f33c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Given an array of strings `words`, return the first palindromic string in the array. | |
| # If there is no such string, return an empty string `""`. | |
| def firstPalindrome(words): | |
| """ | |
| :type words: List[str] | |
| :rtype: str | |
| """ | |
| def is_palindrome(s): | |
| """ | |
| :type s: str | |
| :rtype: bool | |
| """ | |
| return s == s[::-1] | |
| for word in words: | |
| if is_palindrome(word): | |
| return word | |
| return "" | |
| # Test cases | |
| words1 = ["abc","car","ada","racecar","cool"] | |
| words2 = ["notapalindrome","racecar"] | |
| words3 = ["def","ghi"] | |
| print(firstPalindrome(words1)) # Output: "ada" | |
| print(firstPalindrome(words2)) # Output: "racecar" | |
| print(firstPalindrome(words3)) # Output: "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment