-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
47 lines (39 loc) · 1.39 KB
/
Result.java
File metadata and controls
47 lines (39 loc) · 1.39 KB
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
45
46
47
package hackrank.algorithm.string.thrones;
import java.util.HashMap;
import java.util.Map;
/**
* @see <a href="https://www.hackerrank.com/challenges/game-of-thrones">Game of Thrones</a>
*/
public class Result {
/**
* @param s value (lowercase ASCII letters only) to check for a palindromic permutation
* @return "YES" if there is a palindromic permutation of {@code s}, else "NO".
*/
public static String gameOfThrones(String s) {
Map<Character, Integer> counts = countChars(s);
int charsOddCount = 0;
for (int count : counts.values()) {
if (count % 2 == 0) {
continue; // char has an even count and can be evenly split between each side of palindrome
}
charsOddCount++;
if (charsOddCount > 1) {
// at least two characters with odd counts. distribution is uneven and a palindrome is not possible
return "NO";
}
}
return "YES";
}
private static Map<Character, Integer> countChars(String value) {
Map<Character, Integer> counts = new HashMap<>();
for (char c : value.toCharArray()) {
if (counts.containsKey(c)) {
int count = counts.get(c);
counts.put(c, count + 1);
} else {
counts.put(c, 1);
}
}
return counts;
}
}