import java.util.Scanner;
import java.util.Arrays;
public class StringIsAnagram {
public static void main (String[] args) {
Scanner input = new Scanner(System.in );
System.out .print("Enter the first string: " );
String s1 = input.next();
System.out .print("Enter the second string: " );
String s2 = input.next();
for (int i = 0 ; i < s1.length(); i++) {
if (!Character.isLetter(s1.charAt(i)) || (!Character.isLetter(s2.charAt(i)))) {
System.out .print("The string must be letters." );
System.exit(0 );
}
}
System.out .println(isAnagram(s1, s2) ? "Anagram" : "Not anagram" );
}
public static boolean isAnagram (String s1, String s2) {
int [] lowerCase1 = new int [26 ];
int [] upperCase1 = new int [26 ];
int [] lowerCase2 = new int [26 ];
int [] upperCase2 = new int [26 ];
countOccusLetters(lowerCase1, upperCase1, s1);
countOccusLetters(lowerCase2, upperCase2, s2);
if (Arrays.equals(lowerCase1, lowerCase2) && Arrays.equals(upperCase1, upperCase2))
return true ;
else
return false ;
}
public static void countOccusLetters (int [] lowerCase, int [] upperCase, String s) {
for (int i = 0 ; i < s.length(); i++) {
if (Character.isLowerCase(s.charAt(i)))
lowerCase[s.charAt(i) - 'a' ]++;
else
upperCase[s.charAt(i) - 'A' ]++;
}
}
}