Anogram
Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For this challenge, the test is not case-sensitive. For example, the anagrams of CAT
are CAT
, ACT
, tac
, TCA
, aTC
, and CtA
.
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
// Complete the function
if (a.length() != b.length())
return false;
String first = a.toLowerCase();
String second = b.toLowerCase();
char[] array1 = first.toCharArray();
char[] array2 = second.toCharArray();
java.util.Arrays.sort(array1);
java.util.Arrays.sort(array2);
return java.util.Arrays.equals(array1, array2);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
Comments
Post a Comment