Open In App

Count of Palindrome Strings in given Array of strings

Last Updated : 13 Jul, 2022
Comments
Improve
Suggest changes
2 Likes
Like
Report

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++
Java Python3 C# JavaScript

 
 


Output
2


 

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.


 


Next Article
Article Tags :
Practice Tags :

Similar Reads