Created
March 14, 2016 07:03
-
-
Save jianminchen/0cfa60bac880f2bba10f to your computer and use it in GitHub Desktop.
Anagram - declare a string using alphabetic chars - 26 chars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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