Skip to content

Instantly share code, notes, and snippets.

@KPCCoiL
Last active August 29, 2015 14:03
Show Gist options
  • Save KPCCoiL/96da9e11e64ee53262c1 to your computer and use it in GitHub Desktop.
Save KPCCoiL/96da9e11e64ee53262c1 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <sstream>
#include <cmath>
#include <unordered_map>
using namespace std;
class PalindromePermutations {
public:
double palindromeProbability(string word) {
int len = word.size();
int odd = len%2;
unordered_map<char,int> cnt;
for (auto i : word) ++cnt[i];
int oddcnt = 0;
for (auto i : cnt) oddcnt += (i.second)%2;
if(oddcnt>odd) return 0.;
double prob = 1;
for (int i = 1; i <= len; i++)
prob /= i;
for (auto i : cnt) {
for (int j = 1; j <= i.second; j++)
prob *= j;
}
for (auto i : cnt) {
for (int j = 1; j <= (i.second)/2; j++)
prob /= j;
}
for (int i = 1; i <= len/2; i++)
prob *= i;
return double(prob);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment