Count of Palindrome Strings in given Array of strings
Last Updated :
13 Jul, 2022
Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the count of all palindromic string in the array.
Examples:
Input: arr[] = {“abc”,”car”,”ada”,”racecar”,”cool”}
Output: 2
Explanation: “ada” and “racecar” are the two palindrome strings.
Input: arr[] = {“def”,”aba”}
Output: 1
Explanation: “aba” is the only palindrome string.
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 count. Follow the steps below to solve the problem:
- Initialize a count variable ans as 0.
- Iterate over the range [0, N) using the variable i and if arr[i] is a palindrome, then increment the value of ans.
- After performing the above steps, print the value of ans as the answer.
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;
}
int PalindromicStrings(string arr[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans++;
}
}
return ans;
}
int main()
{
string arr[]
= { "abc" , "car" , "ada" ,
"racecar" , "cool" };
int N = sizeof (arr) / sizeof (arr[0]);
cout << PalindromicStrings(arr, N);
return 0;
}
|
Java
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 int PalindromicStrings(String []arr,
int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (isPalindrome(arr[i])) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
String []arr
= { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.length;
System.out.print(PalindromicStrings(arr, N));
}
}
|
Python3
def isPalindrome( str ):
l = 0 ;
h = len ( str ) - 1 ;
while (h > l):
if ( str [l] ! = str [h]):
return False ;
l + = 1 ;
h - = 1 ;
return True ;
def PalindromicStrings(arr, N):
ans = 0 ;
for i in range (N):
if (isPalindrome(arr[i])):
ans + = 1 ;
return ans;
if __name__ = = '__main__' :
arr = [ "abc" , "car" , "ada" , "racecar" , "cool" ];
N = len (arr);
print (PalindromicStrings(arr, N));
|
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 int PalindromicStrings( string []arr,
int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans++;
}
}
return ans;
}
public static void Main()
{
string []arr
= { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.Length;
Console.Write(PalindromicStrings(arr, N));
}
}
|
Javascript
<script>
function isPalindrome(s)
{
let a = s;
s = s.split( "" ).reverse().join( "" );
return s == a;
}
function PalindromicStrings(arr, N) {
let ans = 0;
for (let i = 0; i < N; i++) {
if (isPalindrome(arr[i])) {
ans++;
}
}
return ans;
}
let arr = [ "abc" , "car" , "ada" , "racecar" , "cool" ];
let N = arr.length;
document.write(PalindromicStrings(arr, N));
</script>
|
Time Complexity: O(N * W) where W is the average length of the strings
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...