Given a string, determine if a permutation of the string could form a palindrome.
For example,
Show Hint "code"
-> False, "aab"
-> True, "carerac"
-> True.
Show Tags
Show Similar Problems
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null) return false;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
boolean hasMetOdd = false;
for (Character key : map.keySet()) {
if (map.get(key) % 2 == 1) {
if (hasMetOdd) return false;
hasMetOdd = true;
}
}
return true;
}
}
没有评论:
发表评论