-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_permutation.cpp
42 lines (38 loc) · 1.61 KB
/
palindrome_permutation.cpp
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
#include <bitset>
#include <iostream>
bool is_palindrome(std::string input_string) {
std::bitset<256> char_vector;
int num_valid_chars = 0;
for (const char &c : input_string) {
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
char_vector.flip(c);
num_valid_chars++;
}
}
// even nb of chars and all even count
if (num_valid_chars % 2 == 0 && char_vector.none()) {
return true;
}
// odd nb of chars and just one with an odd count
else if (num_valid_chars % 2 == 1 && char_vector.count() == 1) {
return true;
}
return false;
}
int main() {
std::cout << (is_palindrome("aba") == true) << std::endl;
std::cout << (is_palindrome("aab") == true) << std::endl;
std::cout << (is_palindrome("abba") == true) << std::endl;
std::cout << (is_palindrome("aabb") == true) << std::endl;
std::cout << (is_palindrome("a-bba") == true) << std::endl;
std::cout << (is_palindrome("a-bba!") == true) << std::endl;
std::cout << (is_palindrome("tact coa") == true) << std::endl;
std::cout << (is_palindrome("jhsabckuj ahjsbckj") == true) << std::endl;
std::cout << (is_palindrome("able was i ere i saw elba") == true) << std::endl;
std::cout << (is_palindrome("so patient a nurse to nurse a patient so") == false) << std::endl;
std::cout << (is_palindrome("random words") == false) << std::endl;
std::cout << (is_palindrome("not a palindrome") == false) << std::endl;
std::cout << (is_palindrome("no x in nixon") == true) << std::endl;
std::cout << (is_palindrome("azaz") == true) << std::endl;
return 0;
}