forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
73 lines (57 loc) · 2.5 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Problem: https://www.hackerrank.com/challenges/anagram
//Java 8
/*
Initial Thoughts:
Get frequency of first and second half
Count the changes second half needs to become first half
Time complexity: O(n) //Time equivalent to input size
Space complexity: O(1) //The alphabet is limited to 26 char so maps have a constant size
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int T = input.nextInt();
input.nextLine();
tests:
for(int t = 0; t < T; t++)
{
String s = input.nextLine();
if(s.length() % 2 == 1)
{
System.out.println(-1); continue tests;
}
Map<Character,Integer> firstHalf = new HashMap<>();
Map<Character,Integer> secondHalf = new HashMap<>();
//Get frequency of chars in first half
for(int i = 0; i < s.length()/2; i++)
{
if(firstHalf.containsKey(s.charAt(i)))
firstHalf.put(s.charAt(i), firstHalf.get(s.charAt(i)) + 1);
else
firstHalf.put(s.charAt(i), 1);
}
//Get frequency of chars in second half
for(int i = s.length()/2; i < s.length(); i++)
{
if(secondHalf.containsKey(s.charAt(i)))
secondHalf.put(s.charAt(i), secondHalf.get(s.charAt(i)) + 1);
else
secondHalf.put(s.charAt(i), 1);
}
int operations = 0;
//Iterate through second half determining what needs to switch to match it
for(Map.Entry<Character,Integer> letter : secondHalf.entrySet())
{
int f2 = letter.getValue();
int f1 = (firstHalf.get(letter.getKey()) != null) ? firstHalf.get(letter.getKey()) : 0;
//This is when there are not enough chars in the first half to match those in the second half
if(f2 > f1)
operations += (f2 - f1);
}
System.out.println(operations);
}
}
}