2108. Find First Palindromic String in the Array - Explanation

Problem Link



Prerequisites

Before attempting this problem, you should be comfortable with:

  • String Manipulation - Reversing strings and comparing characters
  • Two Pointers - Using left and right pointers to check palindrome properties efficiently
  • Array Iteration - Iterating through arrays and returning early when a condition is met

1. Reverse String

Intuition

A palindrome reads the same forwards and backwards. The simplest way to check this is to reverse the string and compare it to the original. If they match, it's a palindrome. We iterate through the array and return the first string that passes this check.

Algorithm

  1. Iterate through each word in the array.
  2. For each word, create a reversed copy.
  3. Compare the original word with its reversed version.
  4. If they are equal, return that word immediately.
  5. If no palindrome is found after checking all words, return an empty string.
class Solution:
    def firstPalindrome(self, words: List[str]) -> str:
        for w in words:
            if w == w[::-1]:
                return w
        return ""

Time & Space Complexity

  • Time complexity: O(nm)O(n * m)
  • Space complexity: O(m)O(m)

Where nn is the size of the string array wordswords and mm is the average length of a word in the array.


2. Two Pointers

Intuition

Instead of creating a reversed copy (which uses extra space), we can check if a string is a palindrome in place using two pointers. One pointer starts at the beginning, the other at the end. If all corresponding characters match as the pointers move toward each other, the string is a palindrome. This avoids the extra memory needed to store the reversed string.

Algorithm

  1. Iterate through each word in the array.
  2. For each word, use two pointers: l at the start and r at the end.
  3. While the characters at l and r are equal:
    • If l >= r, the word is a palindrome; return it.
    • Move l forward and r backward.
  4. If the characters differ, move to the next word.
  5. If no palindrome is found, return an empty string.
class Solution:
    def firstPalindrome(self, words: List[str]) -> str:
        for w in words:
            l, r = 0, len(w) - 1
            while w[l] == w[r]:
                if l >= r:
                    return w
                l, r = l + 1, r - 1
        return ""

Time & Space Complexity

  • Time complexity: O(nm)O(n * m)
  • Space complexity: O(1)O(1) extra space.

Where nn is the size of the string array wordswords and mm is the average length of a word in the array.


Common Pitfalls

Forgetting to Return an Empty String

A common mistake is forgetting to handle the case where no palindrome exists in the array. The problem requires returning an empty string "" when no palindromic string is found, not null or throwing an error. Always ensure your function has a default return statement after the loop completes.

Continuing Search After Finding the First Palindrome

Since the problem asks for the first palindromic string, you should return immediately upon finding one. A pitfall is iterating through the entire array and collecting all palindromes, then returning the first. This wastes time and misses the point of early termination, which is key to optimal performance.