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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
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.