# Count maximum-length palindromes in a String

Given a string, count how many maximum-length palindromes are present.(It need not be a substring)

**Examples:**

Input : str = "ababa" Output: 2 Explanation : palindromes of maximum of lenghts are : "ababa", "baaab" Input : str = "ababab" Output: 4 Explanation : palindromes of maximum of lenghts are : "ababa", "baaab", "abbba", "babab"

**Approach** A palindrome can be reprsented as **“str + t + reverse(str)”**.

**Note**: **“t”** is empty for even length palindromic strings.

Calculate in how many ways “str” can be made and then multiply with “t” (number of single characters left out).

Let c_{i} be the number of occurrences of character in the string. Consider the following cases:

1. If c_{i} is even. Then a half of every maximum palindrome will contain exactly letters f_{i} = c_{i} / 2.

2.If c_{i} is odd. Then a half of every maximum palindrome will contain exactly letters f_{i} = (c_{i} – 1)/ 2.

Let k be the number of odd c_{i}. If k=0, the maximum palindromes length will be even; otherwise it will be odd and there will be exactly k possible middle letters i.e., we can set this letter to the middle of palindrome.

The number of permutations of n objects with n_{1} identical objects of type 1, n_{2} identical objects of type 2,, and n3 identical objects of type 3 is ** n! / (n1! * n2! * n3!)** .

So here we have total number of characters as f_{a}+f_{b}+f_{a}+…….+f_{y}+f_{z} . So number of permutation is **(f _{a}+f_{b}+f_{a}+…….+f_{y}+f_{z}+)! / f_{a}! f_{b}!…f_{y}!f_{z}!.**

Now If K is not 0, it’s obvious that the answer is k * (f_{a}+f_{b}+f_{a}+…….+f_{y}+f_{z}+)! / f_{a}! f_{b}!…f_{y}!f_{z}!

Below is the implementation of the above.

## C++

`// C++ implementation for counting ` `// maximum length palindromes ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// factorial of a number ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `ans = ans * i; ` ` ` `return` `(ans); ` `} ` ` ` `// function to count maximum length palindromes. ` `int` `numberOfPossiblePallindrome(string str, ` `int` `n) ` `{ ` ` ` `// Count number of occurrence ` ` ` `// of a charterer in the string ` ` ` `unordered_map<` `char` `, ` `int` `> mp; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[str[i]]++; ` ` ` ` ` `int` `k = 0; ` `// Count of singles ` ` ` `int` `num = 0; ` `// numerator of result ` ` ` `int` `den = 1; ` `// denominator of result ` ` ` `int` `fi; ` ` ` `for` `(` `auto` `it = mp.begin(); it != mp.end(); ++it) ` ` ` `{ ` ` ` `// if frequency is even ` ` ` `// fi = ci / 2 ` ` ` `if` `(it->second % 2 == 0) ` ` ` `fi = it->second / 2; ` ` ` ` ` `// if frequency is odd ` ` ` `// fi = ci - 1 / 2. ` ` ` `else` ` ` `{ ` ` ` `fi = (it->second - 1) / 2; ` ` ` `k++; ` ` ` `} ` ` ` ` ` `// sum of all frequencies ` ` ` `num = num + fi; ` ` ` ` ` `// product of factorial of ` ` ` `// every frequency ` ` ` `den = den * fact(fi); ` ` ` `} ` ` ` ` ` `// if all character are unique ` ` ` `// so there will be no pallindrome, ` ` ` `// so if num != 0 then only we are ` ` ` `// finding the factorial ` ` ` `if` `(num != 0) ` ` ` `num = fact(num); ` ` ` ` ` `int` `ans = num / den; ` ` ` ` ` `if` `(k != 0) ` ` ` `{ ` ` ` `// k are the single ` ` ` `// elements that can be ` ` ` `// placed in middle ` ` ` `ans = ans * k; ` ` ` `} ` ` ` ` ` `return` `(ans); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `char` `str[] = ` `"ababab"` `; ` ` ` `int` `n = ` `strlen` `(str); ` ` ` `cout << numberOfPossiblePallindrome(str, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation for counting

# maximum length palindromes

import math as mt

# factorial of a number

def fact(n):

ans = 1

for i in range(1, n + 1):

ans = ans * i

return (ans)

# function to count maximum length palindromes.

def numberOfPossiblePallindrome(string, n):

# Count number of occurrence

# of a charterer in the string

mp = dict()

for i in range(n):

if string[i] in mp.keys():

mp[string[i]] += 1

else:

mp[string[i]] = 1

k = 0 # Count of singles

num = 0 # numerator of result

den = 1 # denominator of result

fi = 0

for it in mp:

# if frequency is even

# fi = ci / 2

if (mp[it] % 2 == 0):

fi = mp[it] // 2

# if frequency is odd

# fi = ci – 1 / 2.

else:

fi = (mp[it] – 1) // 2

k += 1

# sum of all frequencies

num = num + fi

# product of factorial of

# every frequency

den = den * fact(fi)

# if all character are unique

# so there will be no pallindrome,

# so if num != 0 then only we are

# finding the factorial

if (num != 0):

num = fact(num)

ans = num //den

if (k != 0):

# k are the single

# elements that can be

# placed in middle

ans = ans * k

return (ans)

# Driver Code

string = “ababab”

n = len(string)

print(numberOfPossiblePallindrome(string, n))

# This code is contributed by

# Mohit kumar 29

**Output:**

4

**Time Complexity :** O(n)

## Recommended Posts:

- Count special palindromes in a String
- Check if suffix and prefix of a string are palindromes
- Split a string in equal parts such that all parts are palindromes
- Find the count of palindromic sub-string of a string in its sorted form
- Count subsequences in first string which are anagrams of the second string
- Count occurrences of a string that can be constructed from another given string
- Program to print all palindromes in a given range
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Count of 'GFG' Subsequences in the given string
- Count words in a given string
- Count All Palindrome Sub-Strings in a String | Set 2
- Count of sub-strings of length n possible from the given string
- Count the pairs of vowels in the given string
- Count of occurrences of a "1(0+)1" pattern in a string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.