Find First Palindromic String in the Array (LeetCode 2108): Solution and Explanation

Easy
Two Pointers
String
Array

Walk through the words in order and return the first one that reads the same backwards. If none does, return the empty string.

Intuition

The order of the words matters, so the scan must go from the first word to the last and stop at the first palindrome. Later palindromes never matter.

To test one word, compare its outermost letters, then the next pair in, and so on. A word is a palindrome exactly when every such pair matches.

Most words that are not palindromes fail on their first or second pair, so checking from the outside in with early exit is cheaper than building the reversed word and comparing whole strings.

Approach: Two-pointer check per word

  1. For each word, set left = 0 and right = len(word) - 1.
  2. While left < right and the letters at both pointers are equal, move left right and right left.
  3. If the pointers met or crossed, every pair matched: return this word.
  4. Otherwise there was a mismatch, so continue with the next word.
  5. After the loop, return "".

Step-by-step example

words = ["abc","car","ada","racecar","cool"]
  1. "abc": 'a' and 'c' differ on the first pair, so it is rejected.
  2. "car": 'c' and 'r' differ, rejected.
  3. "ada": 'a' matches 'a'. The pointers move to index 1 and meet on 'd'.
  4. The pointers met without a mismatch, so "ada" is returned. "racecar" is never checked.

Code: Python, Java, C++

1class Solution:
2    def firstPalindrome(self, words: List[str]) -> str:
3        for word in words:
4            left, right = 0, len(word) - 1
5            while left < right and word[left] == word[right]:
6                left += 1
7                right -= 1
8            if left >= right:
9                return word
10        return ""
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n * m)

Each of the n words is checked in at most m / 2 comparisons, where m is the longest word length.

Space

O(1)

Only the two pointers are stored; no reversed copy of a word is built.

Other approaches

Compare with the reversed word (time O(n * m), space O(m))

Return the first word with word == word[::-1]. It is a one-liner and has the same time bound, but it builds a reversed copy of every word and never exits early on a first-pair mismatch.

Recursive check (time O(n * m), space O(m))

A word is a palindrome if its ends match and the inside is a palindrome. It reads nicely but uses O(m) stack depth.

Common mistakes

  • Returning the longest or the last palindrome instead of the first one.
  • Returning null or None instead of the empty string when there is no palindrome.
  • Looping while left <= right and comparing the middle letter with itself is harmless, but forgetting that a one-letter word is a palindrome is not.
  • Using a loop condition that stops on a match instead of a mismatch.

Frequently asked questions

Is a single letter a palindrome?

Yes. left and right start equal, the loop never runs, and the check left >= right passes.

Why not just compare the word with its reverse?

That is fine for these limits. The two-pointer version avoids allocating a new string per word and stops at the first mismatch.

Do I need to handle uppercase letters or spaces?

No. Every word is lowercase letters only, unlike Valid Palindrome where non-letters must be skipped.