Skip to content

Instantly share code, notes, and snippets.

@preston-56
Created April 9, 2024 14:55
Show Gist options
  • Select an option

  • Save preston-56/e38a0356fc85157a81cb14cba23f33c2 to your computer and use it in GitHub Desktop.

Select an option

Save preston-56/e38a0356fc85157a81cb14cba23f33c2 to your computer and use it in GitHub Desktop.
# 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