forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_266.java
35 lines (26 loc) · 1.03 KB
/
_266.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
/**Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code" -> False, "aab" -> True, "carerac" -> True.
Hint:
Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?*/
public class _266 {
public boolean canPermutePalindrome(String s) {
char[] chars = s.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(char c : chars){
if(!map.containsKey(c)) map.put(c, 1);
else map.put(c, map.get(c)+1);
}
int evenCount = 0;
for(Map.Entry<Character, Integer> e : map.entrySet()){
if(e.getValue() % 2 != 0) evenCount++;
if(evenCount > 1) return false;
}
return true;
}
}