-
Notifications
You must be signed in to change notification settings - Fork 28
/
PermutationPalindrome.java
44 lines (35 loc) · 1.31 KB
/
PermutationPalindrome.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
36
37
38
39
40
41
42
43
44
package com.anthonynsimon.algorithms.strings;
public final class PermutationPalindrome {
public static boolean isPermutationOfPalindrome(String str) {
// Remove punctuation, whitespace, make all lowercase...
str = cleanUp(str);
if (str.length() == 0) {
return false;
}
// Assume ASCII
char[] charFrequencies = new char[128];
// Count the number of times each char appears
for (int i = 0; i < str.length(); i++) {
charFrequencies[(int) str.charAt(i)] += 1;
}
boolean hasMultipleOddChars = false;
for (int i = 0; i < charFrequencies.length; i++) {
// If there is more than one char with odd counts,
// string cannot form a palindrome
if (charFrequencies[i] % 2 != 0) {
if (hasMultipleOddChars) {
return false;
} else {
hasMultipleOddChars = true;
}
}
}
return true;
}
private static String cleanUp(String str) {
Sanitizer sanitizer = new Sanitizer();
char[] blacklist = {',', '-', '!', '.', ' ', ';', '\'', ':', '"', '?'};
String sanitizedStr = sanitizer.sanitize(str, blacklist);
return sanitizedStr.toLowerCase();
}
}