Last active
August 29, 2015 14:03
-
-
Save KPCCoiL/96da9e11e64ee53262c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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