問題
給定一個字符串,判斷該字符串中是否可以通過重新排列組合,形成一個迴文字符串。
例子
思路
-
方法1
奇數個的字符爲1個或者0個
-
方法2
代碼
//方法1
class Solution {
public boolean canPermutePalindrome(String s) {
int[] arr = new int[128];
for(int i=0;i<s.length(); i++) {
char c = s.charAt(i);
if(arr[c]==1) arr[c]=0;
else arr[c]=1;
}
int res=0;
for(int n: arr){
res +=n;
}
return res==0||res==1?true:false;
}
}
//方法2