Skip to content

Instantly share code, notes, and snippets.

@jordan-cutler
Created January 5, 2017 01:10
Show Gist options
  • Save jordan-cutler/db53a7c76f230d7869476a903fedb5c8 to your computer and use it in GitHub Desktop.
Save jordan-cutler/db53a7c76f230d7869476a903fedb5c8 to your computer and use it in GitHub Desktop.
public static boolean isAnagramOfPalindrome(String test) {
HashSet<Character> vals = new HashSet<>();
int length = test.length();
for (int i = 0; i < length; i++) {
if (Character.isWhitespace(test.charAt(i))) {
// do nothing
}
else if (vals.contains(test.charAt(i))) {
vals.remove(test.charAt(i));
}
else {
vals.add(test.charAt(i));
}
}
// by the end of the loop we will have no characters or 1 if we should return true
if (vals.size() == 0 || vals.size() == 1) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment