YODA - Yoda Goes Palindromic !

According to a very famous web site, which in this case we will trust, defines a palindrome as ‘a word, phrase, verse, or sentence that reads the same backward or forward’. For example, the phrase A man, a plan, a canal, Panama! is a palindrome. Actually, writing texts consisting of only palindromes is part of a literary technique called constrained writing.

Now imagine the wise Yoda, the master of all, whose proficiency putting words together in sentences is one of his well-kwnown abilities. He is now interested in enriching his long- lasting, and maybe boring, inactivity periods by ‘composing’ palindromic sentences. That is, he has plans to use only palindromic sentences for his chats. For this matter, he needs to practice. The first task in his practice plan is to count all the palindromes that can be arranged out of a collection of characters.

Today, you will be Yoda’s assistant for this first task. Your only mission is to, given a sequence of characters, determine how many palindromes can be obtained with some of the characters in the sequence; you will only take into account uppercase or lowercase letters. Put in other way, you need to determine how many permutations of a give sequence of characters are palindromes. Your solution will help definitively master Yoga.


The input consists of several test cases, one per line. For each test case, the input consist of a sequence of ASCII characters.


For each test case you should print in a single line, and according to the order of the test cases, the total number of palindromes generated by the input sequence of ASCII characters. For your purpose, you should only consider uppercase or lowercase characters appearing in the input; any other character should be ignored in the calculations. Uppercase and lowercase characters are not considered different; for example, A and a should not be considered different. In any case, the total number of palindromes will not exceed the number e^43 , where e is approximately 2.71828. Remember that the empty sequence is a palindrome itself.


A man, a plan, a canal, Panama!
12[’;. =1


hide comments
Naman Goyal: 2015-05-15 22:31:40

Really good problem. I solved it with some combinatorics but calculating the term without overflowing and with basic C++ datatype was interesting.

(Tjandra Satria Gunawan)(曾毅昆): 2015-01-16 12:33:44

Both RAJDEEP GUPTA's comments and problem description is right, got AC with both considering digits and not considering digits. I got 3 WAs before because I didn't handle the '\t' in the input. (I use getchar_unlocked, not gets)..

Chika: 2014-05-31 19:34:44

Last edit: 2014-05-31 20:30:17
Paul Redman: 2012-01-27 14:32:53

The problem description is correct, not the comments: ignore digits, 0-length sequence should give answer 1.

rishabh: 2012-01-16 19:00:30

answer for "[ ]" is 1, for an empty string is also 1.
take care of that.

RAJDEEP GUPTA: 2012-01-12 08:35:28

please note the following:
1. take input as while(gets(string))
2.you must consider digits too. Answer for "aa11Bbb" is 6.
3. characters other than alphabets and digits should be ignored. Answer for "[]" is 0.

Prakhar Jain: 2012-01-02 21:15:46

output for abaaba is 3...

Last edit: 2012-01-02 21:23:29
biQar: 2011-08-27 17:56:27

@Prabakaran - u should read input until the END OF FILE.

biQar: 2011-08-27 17:55:11

@SpojDog - in the 2nd test case "arD,R!A" we got the string "arDRA". As, this is not case sensitive, we can consider "ardra". Among all the permutations of this string, "ardra" & "radar" ar palindromic !! so the ans is 2.

Prabakaran: 2011-01-09 07:42:24

how to end the input?

Added by:Daniel Gómez Didier
Time limit:3.950s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:2007 U.Nacional - Circuito de Maratones ACIS / REDIS