- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to count number of unique palindromes we can make using string characters in Python

Suppose we have a string s, we have to find the number of distinct palindromes we can generate using all characters. If the answer is very large then mod the result by 10^9 + 7.

So, if the input is like s = "xyzzy", then the output will be 2, as we can make "zyxyz" and "yzxzy"

To solve this, we will follow these steps −

m = 10^9+7

char_freq := a map holding each character of s and their frequencies

odd := 0

for each character k and frequency v in char_freq, do

if v mod 2 is 1, then

odd := odd + 1

if odd > 1, then

return 0

half_length := quotient of (size of s) / 2

res := factorial of half_length

dividor := 1

for each character k and frequency v in char_freq, do

dividor := dividor * factorial of (quotient of v/2)

return (quotient of res/dividor) mod m

Let us see the following implementation to get better understanding −

## Example

from math import factorial class Solution: def solve(self, s): m = (10**9+7) char_freq = {} for c in s: char_freq[c] = char_freq.get(c, 0) + 1 odd = 0 for k,v in char_freq.items(): if v % 2 == 1: odd +=1 if odd > 1: return 0 half_length = len(s)//2 res = factorial(half_length) dividor = 1 for k,v in char_freq.items(): dividor *= factorial(v//2) return (res//dividor) % m ob = Solution() print(ob.solve("xyzzy"))

## Input

"xyzzy"

## Output

2

- Related Articles
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to count number of strings we can make using grammar rules in Python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Python Program to Count Number of Lowercase Characters in a String
- Count Number of Lowercase Characters in a String in Python Program
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to count number of palindromes after minimum number of split of the string in C++
- C++ program to count number of dodecagons we can make of size d
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find length of concatenated string of unique characters in Python?
- Program to count number of distinct characters of every substring of a string in Python
- Program to count number of unique subsequences of a string in C++