-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.java
More file actions
58 lines (46 loc) · 2 KB
/
anagram.java
File metadata and controls
58 lines (46 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
/* Sid is obsessed with reading short stories. Being a CS student, he is doing some interesting frequency analysis with the books.
He chooses strings s1 and s2 in such a way that |len(s1)-len(s2)|<=1.
Your task is to help him find the minimum number of characters of the first string he needs to change to enable him to make it
an anagram of the second string. Robot can also re-arrange the alphabets in s1 to make it anagram of s2.
Note: A word x is an anagram of another word y if we can produce y by rearranging the letters of x.
Input Format
The first line will contain an integer, T, representing the number of test cases. Each test case will contain a string having
length len(s1)+len(s2), which will be concatenation of both the strings described above in the problem.
The given string will contain only characters from a to z.
Output Format
An integer corresponding to each test case is printed in a different line, i.e. the number of changes required for each test case.
Print -1 if it is not possible. */
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int anagram(String s){
if(s.length()%2!=0)
return -1;
String s1,s2;
int i,count=0;
int [] a = new int [26];
int [] b = new int [26];
s1 = s.substring(0,s.length()/2);
s2 = s.substring(s.length()/2);
for(i=0;i<s1.length();i++)
{
a[s1.charAt(i)-'a']++;
b[s2.charAt(i)-'a']++;
}
for(i=0;i<26;i++)
count=count+Math.abs(a[i]-b[i]);
return count/2;
}
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);
}
}
}