1.5 Palindrome Permutation
Description
Given a string, determine if a permutation of the string could form a palindrome.
For example, "code" -> False, "aab" -> True, "carerac" -> True.
Method
count the frequency of each character, if there has more than one character's frequency is odd number, it will be false ,otherwise it is true;
Time and Space Complexity
Time : o(n) Space : o(n)
Code
public class Solution {
public boolean canPermutePalindrome(String s) {
HashMap<Character, Integer> map = new HashMap<>();
int count = 0;
for (char c : s.toCharArray()){
if (map.containsKey(c)){
map.put(c, 1 + map.get(c));
} else {
map.put(c, 1);
}
}
for (Integer i : map.values()){
if (i % 2 != 0){
count++;
}
}
if (count > 1){
return false;
}
return true;
}
}