Created
July 1, 2016 19:20
-
-
Save danielrobertson/6e06cafe3a69e9b241e750ef6c31c05d to your computer and use it in GitHub Desktop.
Cracking the Coding Chpt 1.4 PalindromePermutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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