-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.4(PalindromePermutation)Version1.cpp
More file actions
72 lines (63 loc) · 1.35 KB
/
Copy path1.4(PalindromePermutation)Version1.cpp
File metadata and controls
72 lines (63 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Check if a given string is a permutation of a palindrome. A palindrome is a workd that is the same forward and backward.
A permuation is a rearrangment of letters
*/
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
/* Map each char to a num a-0, b-1 */
int getCharNumber(char c){
int val = c;
if(val>='a' && val<='z'){
return val-'a';
}else if(val>='A' && val<='Z'){
return val-'A';
}
return -1;
}
/*count how many times each character appears */
vector<int> buildcharfrequencytable(string phrase){
vector<int> table('z'-'a');
for(int i=0; i<phrase.size(); i++)
{
int x = getCharNumber(phrase[i]);
table[x]++;
}
return table;
}
/*check that no more than one char has odd count */
bool checkMaxOdd (vector <int>table){
bool foundOdd = false;
for(int i=0; i<table.size(); i++){
if(table[i]%2==1){
if(foundOdd){
return false;
}
foundOdd = true;
}
}
return true;
}
bool ispermutationofpalindrom(string phrase){
vector <int> table = buildcharfrequencytable(phrase);
return checkMaxOdd(table);
}
int main()
{
string s;
cin>>s;
vector<int>m;
// m=buildcharfrequencytable(s);
// cout<<m.size();
for(int i=0; i<m.size(); i++){
//cout<<i<<" = "<<m[i]<<endl;
}
bool m1;
m1 = ispermutationofpalindrom(s);
if(m1){
cout<<"yess!";
}
return 0;
}