Count of Palindrome Strings in given Array of strings Last Updated : 13 Jul, 2022 Comments Improve Suggest changes Like Article 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: 2Explanation: "ada" and "racecar" are the two palindrome strings. Input: arr[] = {"def","aba"}Output: 1Explanation: "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++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if given string // is Palindrome or not bool isPalindrome(string& s) { // Copy string s char into string a string a = s; reverse(s.begin(), s.end()); // Check if two string are equal or not return s == a; } // Function to return count // of Palindrome string int PalindromicStrings(string arr[], int N) { int ans = 0; // Loop to find palindrome string for (int i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code int main() { string arr[] = { "abc", "car", "ada", "racecar", "cool" }; int N = sizeof(arr) / sizeof(arr[0]); // Print required answer cout << PalindromicStrings(arr, N); return 0; } Java // java program for the above approach class GFG { // Function to check if given String // is Palindrome or not static boolean isPalindrome(String str) { // Start from leftmost and rightmost corners of str int l = 0; int h = str.length() - 1; // Keep comparing characters while they are same while (h > l) { if (str.charAt(l++) != str.charAt(h--)) { return false; } } return true; } // Function to return all Palindrome String static int PalindromicStrings(String []arr, int N) { int ans = 0; // Loop to find palindrome String for (int i = 0; i < N; i++) { // Checking if given String is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code public static void main(String[] args) { String []arr = { "abc", "car", "ada", "racecar", "cool" }; int N = arr.length; System.out.print(PalindromicStrings(arr, N)); } } // This code is contributed by Rajput-Ji Python3 # Python program for the above approach # Function to check if given String # is Palindrome or not def isPalindrome(str): # Start from leftmost and rightmost corners of str l = 0; h = len(str) - 1; # Keep comparing characters while they are same while (h > l): if (str[l] != str[h]): return False; l += 1; h -= 1; return True; # Function to return all Palindrome String def PalindromicStrings(arr, N): ans = 0; # Loop to find palindrome String for i in range(N): # Checking if given String is # palindrome or not if (isPalindrome(arr[i])): # Update answer variable ans += 1; return ans; # Driver Code if __name__ == '__main__': arr = ["abc", "car", "ada", "racecar", "cool"]; N = len(arr); print(PalindromicStrings(arr, N)); # This code is contributed by 29AjayKumar C# // C# program for the above approach using System; using System.Collections; class GFG { // Function to check if given string // is Palindrome or not static bool isPalindrome(string str) { // Start from leftmost and rightmost corners of str int l = 0; int h = str.Length - 1; // Keep comparing characters while they are same while (h > l) { if (str[l++] != str[h--]) { return false; } } return true; } // Function to return all Palindrome string static int PalindromicStrings(string []arr, int N) { int ans = 0; // Loop to find palindrome string for (int i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code public static void Main() { string []arr = { "abc", "car", "ada", "racecar", "cool" }; int N = arr.Length; Console.Write(PalindromicStrings(arr, N)); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // Javascript program for the above approach // Function to check if given string // is Palindrome or not function isPalindrome(s) { // Copy string s char into string a let a = s; s = s.split("").reverse().join(""); // Check if two string are equal or not return s == a; } // Function to return count // of Palindrome string function PalindromicStrings(arr, N) { let ans = 0; // Loop to find palindrome string for (let i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code let arr = ["abc", "car", "ada", "racecar", "cool"]; let N = arr.length; // Print required answer document.write(PalindromicStrings(arr, N)); // This code is contributed by saurabh_jaiswal. </script> Output2 Time Complexity: O(N * W) where W is the average length of the stringsAuxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Count of Palindrome Strings in given Array of strings C code_r Follow Improve Article Tags : Strings Algo Geek DSA Algo-Geek 2021 palindrome +1 More Practice Tags : palindromeStrings Similar Reads 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 Count substrings of a given string whose anagram is a palindrome Given a string S of length N containing only lowercase alphabets, the task is to print the count of substrings of the given string whose anagram is palindromic. Examples: Input: S = "aaaa"Output: 10Explanation:Possible substrings are {"a", "a", "a", "a", "aa", "aa", "aa", "aaa", "aaa", "aaaa"}. Sinc 10 min read Count of Palindromic substrings in an Index range Given a string str of small alphabetic characters other than this we will be given many substrings of this string in form of index tuples. We need to find out the count of the palindromic substrings in given substring range. Examples: Input : String str = "xyaabax" Range1 = (3, 5) Range2 = (2, 3) Ou 11 min read Count of non-palindromic strings of length M using given N characters Given two positive integers N and M, the task is to calculate the number of non-palindromic strings of length M using given N distinct characters. Note: Each distinct character can be used more than once. Examples: Input: N = 3, M = 2 Output: 6 Explanation: Since only 3 characters are given, those 3 8 min read 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 Count Palindromic Substrings in a Binary String Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's. Examples: Input: S = "011"Output: 4Explanation: "0", "1", "1" and "11" are the palindromic substrings. Input: S = "0" Output: 1Explanatio 7 min read Find the count of palindromic sub-string of a string in its sorted form Given string str consisting of lowercase English alphabets, the task is to find the total number of palindromic sub-strings present in the sorted form of str. Examples: Input: str = "acbbd" Output: 6 All palindromic sub-string in it's sorted form ("abbcd") are "a", "b", "b", "bb", "c" and "d". Input 5 min read All distinct palindromic sub-strings of a given string Given a string str of lowercase ASCII characters. The task is to find all the distinct continuous palindromic sub-strings which are present in the string str. Examples: Input: str = "abaaa"Output: [ "a", "aa", "aaa", "aba", "b" ]Explanation: All 5 distinct continuous palindromic sub-strings are list 15+ min read Form minimum number of Palindromic Strings from a given string Given a string S, the task is to divide the characters of S to form minimum number of palindromic strings. Note: There can be multiple correct answers. Examples: Input: S = "geeksforgeeks"Output: {eegksrskgee, o, f} Explanation: There should be at least 3 strings "eegksrskgee", "o", "f". All 3 forme 12 min read Count All Palindromic Subsequence in a given String Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s.Example: Input: s = "abcd"Output: 4Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d"Input: s = "aab"Output: 4Explanation: palindromic subsequenc 15+ min read Like