Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created July 1, 2016 19:20
Show Gist options
  • Save danielrobertson/6e06cafe3a69e9b241e750ef6c31c05d to your computer and use it in GitHub Desktop.
Save danielrobertson/6e06cafe3a69e9b241e750ef6c31c05d to your computer and use it in GitHub Desktop.
Cracking the Coding Chpt 1.4 PalindromePermutation
// palindrome will have even letter counts and at most one odd letter count
boolean isPalindromPermutation(String input) {
Map<String, Integer> frequency = new HashMap<String, Integer>();
for(String s : input.split("")) {
if(!frequency.containsKey(s)) {
frequency.put(s, 1);
} else {
frequency.put(s, 1 + frequency.get(s));
}
}
// at most one odd count
int oddCount = 0;
for(Map.Entry<String, Integer> entry : frequency.entrySet()) {
if(entry.getValue() % 2 != 0) {
++oddCount;
if(oddCount > 1) {
return false;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment