Find First Palindromic String in the Array

Updated on 28 May, 2025
Find First Palindromic String in the Array header image

Problem Statement

In this task, we are provided with an array of strings named words. Our goal is to find and return the first palindromic string from this array. The definition of a palindromic string is straightforward: a string that reads the same way both forwards and backwards. If no such string exists in the array, the function should return an empty string "".

Examples

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.

Approach and Intuition

The problem is essentially about checking each string in the given array and determining if it is palindromic. Let's break down the approach step-by-step to tackle this problem:

  1. Understand the Definition of Palindrome:
    A palindrome is a sequence that reads the same backward as forward. Examples include "radar", "level", "rotor".

  2. Iterate Through the Array:
    Begin by iterating through each string in the array.

  3. Check Each String:
    For each string in the loop, check whether it is a palindrome. This can be achieved by comparing the string to its reverse. In Python, str == str[::-1] can be used.

  4. Return the First Palindromic String:
    As soon as a palindromic string is found, return it. There’s no need to check subsequent strings.

  5. Handle Cases with No Palindromes:
    If the loop completes without finding any palindromic strings, return an empty string "".

  • Performance Considerations:
    Given the constraints, the worst-case scenario involves checking up to 100 strings of length up to 100—a manageable computational task on modern hardware.

By following these steps, the function efficiently identifies the first palindromic string in the array, or returns an empty string if none are found, adhering to the problem's requirements.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    bool checkPalindrome(string& str) {
        int leftIndex = 0;
        int rightIndex = str.length() - 1;
        
        while (leftIndex <= rightIndex) {
            if (str[leftIndex] != str[rightIndex]) {
                return false;
            }
            leftIndex++;
            rightIndex--;
        }
        return true;
    }
    
    string findFirstPalindrome(vector<string>& wordList) {
        for (string word : wordList) {
            if (checkPalindrome(word)) {
                return word;
            }
        }
        
        return "";
    }
};

Explore the prewritten C++ class designed to find the first palindromic string within a vector of strings. This solution includes two primary functions: one to check if a given string is palindromic and another to iterate through a list of strings to find and return the first palindrome.

  • The checkPalindrome function examines whether a string is identical when read forwards and backwards. It processes by comparing characters from the start and end of the string, moving inward until either a mismatch is found, or all characters are validated to be symmetrical.
  • The findFirstPalindrome function iterates through the input vector of strings. For each string, it invokes the checkPalindrome function. If a palindrome is found, it immediately returns this string. Otherwise, after checking all strings, it returns an empty string if no palindromic string exists in the list.

Use the given class structure to integrate this palindrome-checking functionality into broader applications or use it to process lists of strings effectively to find palindromic entries.

java
class Solution {
    private boolean checkPalindrome(String word) {
        int start = 0;
        int finish = word.length() - 1;

        while (start <= finish) {
            if (word.charAt(start) != word.charAt(finish)) {
                return false;
            }
            start++;
            finish--;
        }
        return true;
    }

    public String firstPalindrome(String[] words) {
        for (String word : words) {
            if (checkPalindrome(word)) {
                return word;
            }
        }
        return "";
    }
}

The provided Java code defines a method for finding the first palindromic string in an array of strings. The solution consists of two main functions:

  • checkPalindrome(String word): This private helper method checks if a given string is a palindrome by comparing characters symmetrically from both ends of the string. If any characters do not match, it returns false. It returns true if all pairs of characters are equal up to the middle of the string.

  • firstPalindrome(String[] words): This public method iterates through an array of strings. For each string, it calls the checkPalindrome method. If this method returns true, indicating the string is a palindrome, the string is immediately returned. If none of the strings in the array are palindromic, the method returns an empty string.

The algorithm ensures efficiency by immediately returning the first palindromic string, thus avoiding unnecessary checks for the remainder of the array once a palindrome is found. This approach optimizes performance, especially in scenarios where a palindrome exists early in the array. If no palindrome is found, the function efficiently concludes by returning an empty string.

Comments

No comments yet.