311. 重构回文
给定一个字符串,判断此字符串能否通过交换某些字符使之变成一个回文串
样例
样例 1:
输入:s = “abc"
输出:
false
样例 2:
输入:s = “aab"
输出:
true
注意事项
-
1<=s.size()<=1000000 1 < = s . s i z e ( ) < = 1 0 0 0 0 0 0
-
字符串仅包含大小写字符
int[] reslut=new int[123];
for (int i = 0; i < s.length(); i++) {
reslut[s.charAt(i)]++;
}
int flag=0;
for (int i = 64; i < reslut.length; i++) {
if (reslut[i]%2!=0){
flag++;
if (flag==2)return false;
}
}
return true;
public class Solution {
/**
* @param s: A string containing only uppercase and lowercase letters
* @return: Judge whether it can become a palindrome
*/
public boolean isPalindrome(String s) {
HashSet<Character> hashSet=new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (!hashSet.add(s.charAt(i))){
hashSet.remove(s.charAt(i));
}
}
return hashSet.size()<2;
}
}