-
Notifications
You must be signed in to change notification settings - Fork 3
/
Anagram.java
45 lines (40 loc) · 1.51 KB
/
Anagram.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
45
// https://www.hackerrank.com/challenges/anagram/problem
package strings;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
while (queries-- > 0) {
String string = scanner.next();
System.out.println(minimumChanges(string));
}
}
private static int minimumChanges(String string) {
if (string.length() % 2 == 1) {
return -1;
}
String first = string.substring(0, string.length() / 2);
String second = string.substring(string.length() / 2);
Map<Character, Integer> frequencies1 = frequencies(first);
Map<Character, Integer> frequencies2 = frequencies(second);
int changes = 0;
for (Map.Entry<Character, Integer> entry : frequencies2.entrySet()) {
changes += Math.max(
entry.getValue() - frequencies1.getOrDefault(entry.getKey(), 0),
0
);
}
return changes;
}
private static Map<Character, Integer> frequencies(String string) {
Map<Character, Integer> frequencies = new HashMap<>();
for (int index = 0 ; index < string.length() ; index++) {
char character = string.charAt(index);
frequencies.put(character, frequencies.getOrDefault(character, 0) + 1);
}
return frequencies;
}
}