forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
40 lines (33 loc) · 1017 Bytes
/
Solution.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int anagram(String s){
if (s.length() % 2 != 0) { return -1; }
int[] cache = new int[70];
for (int i = 0; i < s.length() / 2; i++) {
cache[Character.getNumericValue(s.charAt(i))]++;
}
for (int i = s.length() / 2; i < s.length(); i++) {
if (cache[Character.getNumericValue(s.charAt(i))] > 0) {
cache[Character.getNumericValue(s.charAt(i))]--;
}
}
int counter = 0;
for (int i = 0; i < 70; i++) {
counter += cache[i];
}
return counter;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
String s = in.next();
int result = anagram(s);
System.out.println(result);
}
}
}