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 <= 100
1 <= words[i].length <= 100
words[i]
consists only of lowercase English letters.The problem asks to find the first palindromic string within a given array of strings. A palindromic string reads the same forwards and backward. The solution involves iterating through the array and checking each string for palindrome properties.
The most efficient approach is to iterate through the array of strings and check each string for the palindrome property. For each string, we can use a two-pointer technique to compare characters from the beginning and end, moving towards the middle. If all corresponding characters match, the string is a palindrome, and we return it. If no palindrome is found after iterating through all strings, we return an empty string.
n
. This contributes O(n)
to the time complexity.m/2
times, where m
is the length of the current string. In the worst case, we might need to check all strings, so we consider the average length of strings to be m
. Therefore, the palindrome check contributes O(m)
time complexity.O(n * m)
, where n
is the number of strings and m
is the average length of a string. In the best-case scenario (the first string is a palindrome), the complexity is O(m).The algorithm uses a constant amount of extra space regardless of the input size. We use a couple of pointers for the palindrome check and some temporary variables. Therefore, the space complexity is O(1), constant space.
def firstPalindrome(words: list[str]) -> str:
"""
Finds the first palindromic string in an array of strings.
Args:
words: A list of strings.
Returns:
The first palindromic string in the array, or an empty string if none exists.
"""
for word in words:
if word == word[::-1]: # Efficient palindrome check using slicing
return word
return ""
The implementations in other languages follow a similar structure. They iterate through the array and use a two-pointer approach or string reversal to identify palindromes. The core logic remains consistent across different programming languages. The code examples provided in the original response show implementations in Java, C++, Go, TypeScript, Rust, and C. These examples also demonstrate similar time and space complexities.
words1 = ["abc", "car", "ada", "racecar", "cool"]
print(firstPalindrome(words1)) # Output: ada
words2 = ["notapalindrome", "racecar"]
print(firstPalindrome(words2)) # Output: racecar
words3 = ["def", "ghi"]
print(firstPalindrome(words3)) # Output:
This demonstrates how the function correctly identifies the first palindromic string or returns an empty string when no palindrome exists.