Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 14, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/0cfa60bac880f2bba10f to your computer and use it in GitHub Desktop.
Save jianminchen/0cfa60bac880f2bba10f to your computer and use it in GitHub Desktop.
Anagram - declare a string using alphabetic chars - 26 chars
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int solve(String a, String b) {
if (a.length() != b.length()) {return -1;}
String alph = "abcdefghijklmnopqrstuvwxyz";
char[] ac = a.toCharArray();
char[] bc = b.toCharArray();
int[] counts = new int[26];
for (int i = 0; i < ac.length; i++) {
counts[alph.indexOf(ac[i])]++;
counts[alph.indexOf(bc[i])]--;
}
int answer = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] > 0) {answer+=counts[i];}
}
return answer;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tests = in.nextInt();
in.nextLine();
for (int i = 0; i < tests; i++) {
String next = in.nextLine();
System.out.println(solve(next.substring(0, next.length() / 2), next.substring(next.length() / 2, next.length())));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment