Open In App

Find First Palindromic String in Array

Last Updated : 11 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of strings arr, the task is to return the first palindromic string in the array. If there is no such string, return an empty string "".

Example:

Input: arr = ["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.

Input: arr = ["notapalindrome","racecar"]
Output: "racecar"
Explanation: The first and only string that is palindromic is "racecar".

Approach:

To solve this problem, we need to iterate through each string in the array arr and check if it is a palindrome. A string is a palindrome if it reads the same forwards and backwards. We can do this by comparing the characters from the beginning and the end of the string moving towards the center.

Steps-by-step approach:

  • Create a helper function to check if a string is a palindrome.
  • Iterate through each string in the arr array.
    • For each string, use the helper function to check if it is a palindrome.
      • If a palindromic string is found, return it immediately.
      • If no palindromic string is found by the end of the array, return an empty string "".

Below is the implementation of the above approach:

C++
#include <bits/stdc++.h>
using namespace std;

// Helper function to check if a string is palindromic
bool isPalindrome(const string& s)
{
    int left = 0;
    int right = s.size() - 1;
    while (left < right) {
        if (s[left] != s[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

string firstPalindrome(vector<string>& words)
{
    // Step 2: Iterate through each string in the words
    // array
    for (const string& word : words) {
        // Step 3: Check if the current string is a
        // palindrome
        if (isPalindrome(word)) {
            // Step 4: Return the first palindromic string
            // found
            return word;
        }
    }
    // Step 5: If no palindromic string is found, return an
    // empty string
    return "";
}

// Driver code
int main()
{
    vector<string> words1
        = { "abc", "car", "ada", "racecar", "cool" };
    cout << firstPalindrome(words1) << endl;

    return 0;
}
Java
import java.util.*;

public class Main {

    // Helper function to check if a string is palindromic
    public static boolean isPalindrome(String s) {
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static String firstPalindrome(List<String> words) {
        // Iterate through each string in the words list
        for (String word : words) {
            // Check if the current string is a palindrome
            if (isPalindrome(word)) {
                // Return the first palindromic string found
                return word;
            }
        }
        // If no palindromic string is found, return an empty string
        return "";
    }

    public static void main(String[] args) {
        List<String> words = Arrays.asList("abc", "car", "ada", "racecar", "cool");
        System.out.println(firstPalindrome(words));
    }
}

// This code is contributed by Shivam Gupta

Output
ada

Time Complexity: O(n*m), where n is the number of strings in the array and m is the average length of the strings. This is because for each string, we check if it is a palindrome in O(m) time.

Auxiliary Space: O(1), as we are using a constant amount of extra space for the palindrome check (ignoring the input space).


Next Article
Practice Tags :

Similar Reads