Two strings,  and , a are b 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
.
Function Description
Complete the isAnagram function in the editor.
isAnagram has the following parameters:
- string a: the first string
- string b: the second string
Returns
- boolean: If a and b are case-insensitive anagrams, return true. Otherwise, return false.
Input Format
The first line contains a string a .
The second line contains a string b.
Sample Input
anagram
margana
Sample Output
Anagrams
SOLUTION:
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
char[] aChars = a.toLowerCase().toCharArray();
char[] bChars = b.toLowerCase().toCharArray();
java.util.Arrays.sort(aChars);
java.util.Arrays.sort(bChars);
return java.util.Arrays.equals(aChars, bChars);
}
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");
}
}
FOLLOW FOR MORE QUESTIONS AND SOLUTIONS |Â DIGIT WOOD
Leave a Reply