Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code"
-> False, "aab"
-> True, "carerac"
-> True.
思路:count 奇偶性,把握Palindrome的特性,全部是偶数对,最多只有一个奇数.
用256好转换成char;以后的变种题有用;
class Solution {
public boolean canPermutePalindrome(String s) {
if(s == null || s.length() == 0) {
return false;
}
int[] count = new int[256];
for(int i = 0; i < s.length(); i++) {
count[s.charAt(i)]++;
}
int oddcount = 0;
for(int i = 0; i < 256; i++) {
if(count[i] % 2 == 1) {
oddcount++;
}
}
return oddcount <= 1;
}
}