Skip to content

Commit

Permalink
Two useful methods implemented and using the standart string instead …
Browse files Browse the repository at this point in the history
…of unecessary wide characters
  • Loading branch information
Stas Batururimi authored and asaph committed Feb 18, 2016
1 parent 07c7f15 commit c183b22
Showing 1 changed file with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,63 @@
#include <vector>
#include <iostream>

bool checkMaxOneOdd(std::vector<int> &table)
using namespace std;

int getCharNumber(const char & c){
int a = (int) 'a';
int z = (int) 'z';
int A = (int) 'A';
int Z = (int) 'Z';
int val = (int) c;
if(a <= val && val <= z){
return val - 'a';
}
else if(A <= val && val <= Z){
return val - 'A';
}
return -1;
}


vector <int> buildCharFrequencyTable(string phrase){
vector <int> table(getCharNumber('z') - getCharNumber('a') + 1, 0);
for(char &c : phrase){
int x = getCharNumber(c);
if(x != -1){
table[x]++;
}
}
return table;
}


bool checkMaxOneOdd(vector<int> &table)
{
bool foundOdd = false;
for (auto count : table)
{
if (count % 2 == 1)
{
if (foundOdd)
{
return false;
}
foundOdd = true;
}
}
return true;
}

bool isPermutationOfPalindrome(const string &phrase)
{
bool foundOdd = false;
for (auto count : table)
{
if (count % 2 == 1)
{
if (foundOdd)
{
return false;
}
foundOdd = true;
}
}
return true;
vector<int> table = buildCharFrequencyTable(phrase);
return checkMaxOneOdd(table);
}

bool isPermutationOfPalindrome(const std::wstring &phrase)
{
std::vector<int> table = Common::buildCharFrequencyTable(phrase);
return checkMaxOneOdd(table);
}

void main(std::vector<std::wstring> &args)
{
std::wstring pali = L"Rats live on no evil star";
std::wcout << isPermutationOfPalindrome(pali) << std::endl;
}
int main(int argc, const char *argv[])
{
string pali = "Rats live on no evil star";
string isPermutation = isPermutationOfPalindrome(pali) ? "yes" : "no";
cout << isPermutation << endl;
return 0;
}

0 comments on commit c183b22

Please sign in to comment.