can a string be permuted to form a palindrome?
bool can_form_palindrome(string s) {
if(s.empty()) return false;
int hash[256] = {0};
for(auto c : s) {
hash[c]++;
}
int odd_num = 0;
for(int i=0; i<256; i++) {
if(hash[i] % 2 != 0) {
odd_num++;
if(odd_num > 1) return false;
}
}
return true;
}
// solution 2
bool can_form_palindrome(string s) {
if(s.empty()) return false;
sort(s.begin(), s.end());
int odd_count = 0;
int char_count = 1;
for(int i=1; i<s.size(); i++) {
if(s[i] == s[i-1])
char_count++;
else {
if(char_count % 2 != 0) {
odd_count++;
if(odd_count > 1) return false;
}
char_count = 1;
}
}
return true;
}