forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
41 lines (37 loc) · 813 Bytes
/
Anagram.java
File metadata and controls
41 lines (37 loc) · 813 Bytes
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
/**
* [Anagram](https://www.hackerrank.com/challenges/anagram/problem)
*
*/
import java.util.Scanner;
public class Anagram {
static int anagram(String s) {
int minNoOfChanes = 0;
if ((s.length() & 1) == 1)
return -1;
int charFreq[] = new int[26];
for (int i = 0; i < s.length(); i++) {
int index = s.charAt(i) - 'a';
if (i < s.length() / 2) {
charFreq[index]++;
} else {
charFreq[index]--;
}
}
for (int i = 0; i < 26; i++) {
if (charFreq[i] > 0) {
minNoOfChanes += charFreq[i];
}
}
return minNoOfChanes;
}
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);
in.close();
}
}
}