I am trying to find out the the no of permutations possible for a string that are palindrome.The only way I could think of is finding out the possible permutations and then checking if each one of them is palindrome or not. Is there any better way?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I am trying to find out the the no of permutations possible for a string that are palindrome.The only way I could think of is finding out the possible permutations and then checking if each one of them is palindrome or not. Is there any better way?
Name |
---|
Note that you need only to permute the first half, then the second half must follow the reversed order of the first half. Also, for a given string the exact characters which will form each half is fixed and you need only to find the number of permutations of the characters forming the first half. Here are some examples. String "ABAG" cannot become a palindrome so answer is 0. String "ABBNAAA" contains 4 'A's, 2 'B's and 1 'N'. This means that N is in the middle and each half contains 2 'A's and 1 'B'. And since that makes 3!/(2!*1!), there are 3 permutations of "ABBNAAA" which are palindromes. And a last one, string "ABBA" contains 2 'A's and 2 'B's, meaning that each half contains 1 'A' and 1 'B' whihc gives 2!/(1!*1!) = 2 permutations :)
Is this a programming problem? Or is it a math problem?
If so, what are your constraints? If the string is guaranteed to be shorter than about 11 characters long then you should definitely do the "brute force" solution as you mentioned (try every permutation and check if it's a palindrome), if it is a programming problem.
But, there is most certainly a better way. It seems more like a combinatorics / counting problem than anything else (recall permutations and combinations from high school or in an introductory algebra class).
Once you are thinking this way, here is an example to get you thinking about it. What do the following strings have in common?
abbacac xyyxzxz
They share a similar "pattern" in terms of their characters. The first string has 3 a's, 2 b's and 2 c's. Similarly the second string has 3 x's, 2 y's and 2 z's. Intuitively, these will have the same answer (number of palindromic permutations). Also, the original order doesn't matter (since you will be permuting them.) So the "counts" of the characters are important -- how many of each character appear in the string.
Secondly, a palindrome is special because it is "symmetric" (by definition). Using the "abbacac" example above, can you think of examples of palindromes from this string?
For example: "abcacba" is such a palindrome. You will notice that it has 1 "a" in the middle, and then the remainder of the letters appear around the middle "a" in a mirrored fashion. In fact, ALL palindromes of abbacac have this property:
Try more examples yourself.
Anyway, the point is, since there are an odd number of a's, that odd "a" has to be in the middle. For everything else, there is (and must be) an even number, and half of them appear on the right, and half of them appear on the left.
So, in the above example, we must have 1 a, 1 b, and 1 c appear on the left, and similarly on the right (of the middle a). The order doesn't matter. You can choose any order of them on the left. Once you know the order of the letters on the left, the remaining letters on the right have only one choice (they must appear as the mirror image of the left side).
This holds true in general:
(I won't give you this last part of how to compute the answer. I'm hoping you will remember this from combinatorics classes, or that you will learn something new by trying to derive the formula yourself!)
Good luck.