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”, “racecar”
Explanation: These two are the only palindrome strings in the given array
Input: arr[] = {“def”, “ab”}
Output: -1
Explanation: No palindrome string is present in the given array.
Approach: The solution is based on greedy approach. Check every string of an array if it is palindrome or not and also keep track of the all palindrome string. Follow the steps below to solve the problem:
- Initialize a vector of string ans.
- Iterate over the range [0, N) using the variable i and If arr[i] is a palindrome, then add it to the ans.
- After performing the above steps, print the strings present in ans as the resultant strings.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string& s)
{
string a = s;
reverse(s.begin(), s.end());
return s == a;
}
vector<string> PalindromicStrings(string arr[],
int N)
{
vector<string> ans;
for ( int i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans.push_back(arr[i]);
}
}
return ans;
}
int main()
{
string arr[]
= { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = sizeof (arr) / sizeof (arr[0]);
vector<string> s = PalindromicStrings(arr, N);
if (s.size() == 0)
cout << "-1" ;
for (string st: s)
cout << st << " " ;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static boolean isPalindrome(String str)
{
int l = 0 ;
int h = str.length() - 1 ;
while (h > l)
{
if (str.charAt(l++) != str.charAt(h--))
{
return false ;
}
}
return true ;
}
static ArrayList<String> PalindromicStrings(String []arr,
int N)
{
ArrayList<String> ans = new ArrayList<String>();
for ( int i = 0 ; i < N; i++) {
if (isPalindrome(arr[i])) {
ans.add(arr[i]);
}
}
return ans;
}
public static void main(String args[])
{
String []arr
= { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.length;
ArrayList<String> s = PalindromicStrings(arr, N);
if (s.size() == 0 )
System.out.print( "-1" );
for (String st : s)
System.out.print(st + " " );
}
}
|
Python3
def isPalindrome(s):
a = ""
for ch in s:
a + = ch
s = "".join( reversed (s))
return s = = a
def PalindromicStrings(arr, N):
ans = []
for i in range (N):
if (isPalindrome(arr[i])):
ans.append(arr[i]);
return ans
if __name__ = = '__main__' :
arr = [ "abc" , "car" , "ada" , "racecar" , "cool" ]
N = len (arr)
s = PalindromicStrings(arr, N);
if ( len (s) = = 0 ):
print ( - 1 , end = "")
for st in s:
print (st, end = " " )
|
C#
using System;
using System.Collections;
class GFG
{
static bool isPalindrome( string str)
{
int l = 0;
int h = str.Length - 1;
while (h > l)
{
if (str[l++] != str[h--])
{
return false ;
}
}
return true ;
}
static ArrayList PalindromicStrings( string []arr,
int N)
{
ArrayList ans = new ArrayList();
for ( int i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans.Add(arr[i]);
}
}
return ans;
}
public static void Main()
{
string []arr
= { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.Length;
ArrayList s = PalindromicStrings(arr, N);
if (s.Count == 0)
Console.Write( "-1" );
foreach ( string st in s)
Console.Write(st + " " );
}
}
|
Javascript
<script>
function isPalindrome(s)
{
let a = s;
s = s.split( '' ).reverse().join( '' );
return s == a;
}
function PalindromicStrings(arr,N)
{
let ans = [];
for (let i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans.push(arr[i]);
}
}
return ans;
}
let arr = [ "abc" , "car" , "ada" , "racecar" , "cool" ];
let N = arr.length;
let s = PalindromicStrings(arr, N);
if (s.length == 0)
document.write( "-1" );
for (let st of s)
document.write(st, " " );
</script>
|
Time complexity: O(N * W) where W is the average length of the strings
Auxiliary Space: O(N * W)