Find First Palindromic String in Array
Last Updated :
11 Jun, 2024
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
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).
Similar Reads
Find Last Palindrome String in the given Array Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the last palindromic string in the array. Note: It guarantees that always one palindromic string is present. Examples: Input: arr[] = {"abc", "car", "ada", "racecar", "cool"}O
5 min read
Find all Palindrome Strings in given Array of strings Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to find all palindromic string in the array. Print -1 if no palindrome is present in the given array. Examples: Input: arr[] = {"abc", "car", "ada", "racecar", "cool"}Output: "ada", "ra
6 min read
Palindrome pair in an array of words (or strings) Given an array of strings arr[] of size n, the task is to find if there exists two strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.Examples:Â Input: arr[] = ["geekf", "geeks", "or", "keeg", "abc", "bc"]Outpu
15+ min read
Find if string is K-Palindrome or not | Set 2 Given a string, find out if the string is K-Palindrome or not. A K-palindrome string transforms into a palindrome on removing at most k characters from it.Examples: Input : String - abcdecba, k = 1 Output : Yes String can become palindrome by removing 1 character i.e. either d or e Input : String -
8 min read
Longest Non-palindromic substring Given a string of size n. The task is to find the length of the largest substring which is not a palindrome. Examples: Input : abba Output : 3 Here maximum length non-palindromic substring is 'abb' which is of length '3'. There could be other non-palindromic sub-strings also of length three like 'bb
7 min read