Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".
A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"] Output: "ada" Explanation: The first string that is palindromic is "ada". Note that "racecar" is also palindromic, but it is not the first.
Example 2:
Input: words = ["notapalindrome","racecar"] Output: "racecar" Explanation: The first and only string that is palindromic is "racecar".
Example 3:
Input: words = ["def","ghi"] Output: "" Explanation: There are no palindromic strings, so the empty string is returned.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method involves checking each word one by one to see if it reads the same forwards and backward. As soon as we discover a word that is a palindrome, we can stop and return it. We continue the search until we either find a palindrome or run out of words.
Here's how the algorithm would work step-by-step:
def find_first_palindrome(words):
for word in words:
# Iterate through each word in the input list
is_palindrome = True
left_index = 0
right_index = len(word) - 1
while left_index < right_index:
# Compare characters from both ends until meet
if word[left_index] != word[right_index]:
is_palindrome = False
break
left_index += 1
right_index -= 1
# If the word is a palindrome, return it
if is_palindrome:
return word
# No palindrome was found so return empty string
return ""The most efficient way to find the first palindromic string is to check each word one by one. As soon as we find a word that's a palindrome, we can stop searching and return it immediately. This avoids unnecessary checks.
Here's how the algorithm would work step-by-step:
def find_first_palindrome(words_list):
for word in words_list:
# Check if the current word is a palindrome.
if word == word[::-1]:
# Return the word immediately since it's a palindrome.
return word
# If no palindrome is found, return an empty string.
return ""| Case | How to Handle |
|---|---|
| Null input array | Return null or throw an IllegalArgumentException, depending on the specifications and language conventions. |
| Empty input array | Return an empty string because no palindromic string can exist. |
| Array containing an empty string | An empty string is considered a palindrome; the algorithm should return it if it's the first one found. |
| Array containing a single string | Check if the single string is a palindrome and return it; otherwise, return an empty string. |
| Array with very long strings | The palindrome check should still function correctly but might impact performance; consider optimizing for long string palindrome checks if necessary. |
| Array with no palindromic strings | The algorithm should return an empty string after iterating through the entire array without finding a palindrome. |
| Array with multiple palindromic strings | The algorithm should return the first palindromic string encountered in the array, as per the problem statement. |
| Strings with mixed case or non-alphanumeric characters. | The palindrome check logic should normalize the strings (e.g., convert to lowercase and remove non-alphanumeric characters) before comparison if this is desired behavior. |