Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code"
-> False, "aab"
-> True, "carerac"
-> True.
Solution 1: count the characters, only one char can be odd number.
public class Solution {
public boolean canPermutePalindrome(String s) {
int[] count=new int[256];
for (int i=0; i<s.length(); i++) count[s.charAt(i)]++;
int t=0;
for (int i=0; i<256; i++) {
if (count[i]%2!=0) t++;
}
return t<=1;
}
}
没有评论:
发表评论