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.Problem Overview: You receive an array of lowercase strings. The task is to return the first string that reads the same forward and backward. If none of the words are palindromes, return an empty string.
Approach 1: Two-Pointer Technique (O(n * m) time, O(1) space)
This approach checks each word using the classic palindrome pattern with two pointers. For every string in the array, place one pointer at the beginning and another at the end. Move both pointers inward while the characters match. If a mismatch occurs, the string is not a palindrome. If the pointers cross without mismatches, the word is a palindrome and you immediately return it.
The key insight is that palindrome verification only requires comparing mirrored characters. This avoids creating extra strings or data structures. Since each word of length m may require up to m/2 comparisons and you may scan up to n words, the total complexity becomes O(n * m). Space usage stays O(1) because the check happens in-place. This method is the most efficient and is commonly used when working with two pointers and string problems.
Approach 2: String Reversal Method (O(n * m) time, O(m) space)
Another straightforward approach is reversing each word and comparing it with the original. Iterate through the array, create a reversed version of the current string, and check if it matches the original string. If both are equal, the word is a palindrome and you return it immediately.
This method is easy to implement in languages with built-in string reversal utilities. However, reversing a string requires allocating additional memory proportional to its length. Each reversal takes O(m) time and O(m) space. While the overall time complexity remains O(n * m), the extra memory makes it slightly less optimal than the two-pointer technique.
Recommended for interviews: Interviewers typically expect the two-pointer approach. It demonstrates a strong grasp of palindrome properties and avoids unnecessary memory allocation. The reversal approach is acceptable as a quick baseline, but the in-place pointer comparison shows deeper algorithmic awareness and better space efficiency.
This approach involves checking each string using a two-pointer technique. You set one pointer at the start and one at the end of the string and compare characters. If all characters match until the pointers cross, the string is a palindrome.
The function isPalindrome uses two pointers to check each character from both ends of a string. If they match all through, the string is a palindrome. The firstPalindrome function iterates through the list of words and returns the first palindromic string it finds.
Time Complexity: O(n * m), where n is the number of words and m is the average length of the words.
Space Complexity: O(1) because it uses constant space.
This method leverages string reversal to check if a word is a palindrome. You can reverse the string and compare it with the original; if they are the same, it's a palindrome.
The reverse function creates and returns a reversed version of the input string. In firstPalindrome, for each word in the array, it checks if the word equals its reversed counterpart, indicating it's a palindrome, and returns it.
Time Complexity: O(n * m), where n is the number of words and m is the length of words.
Space Complexity: O(m) due to the additional string for reversal.
| Approach | Complexity |
|---|---|
| Two-Pointer Technique | Time Complexity: O(n * m), where n is the number of words and m is the average length of the words. |
| String Reversal Method | Time Complexity: O(n * m), where n is the number of words and m is the length of words. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer Technique | O(n * m) | O(1) | Best general solution when checking palindromes efficiently without extra memory |
| String Reversal Method | O(n * m) | O(m) | Useful when language provides simple built-in string reversal and memory usage is not critical |
Find First Palindromic String in the Array - Leetcode 2108 - Python • NeetCodeIO • 11,882 views views
Watch 9 more video solutions →Practice Find First Palindromic String in the Array with our built-in code editor and test cases.
Practice on FleetCode