Hackerrank Java Anagrams Solution
.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For example, the anagrams of CAT
are CAT
, ACT
, TAC
, TCA
, ATC
, and CTA
.
Complete the function in the editor. If and are case-insensitive anagrams, print "Anagrams"; otherwise, print "Not Anagrams" instead.
Input Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
The first line contains a string denoting .
The second line contains a string denoting .
Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
- Strings and consist of English alphabetic characters.
- The comparison should NOT be case sensitive.
Output Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
Print "Anagrams" if and are case-insensitive anagrams of each other; otherwise, print "Not Anagrams" instead.
Sample Input 0anagrammargana
Sample Output 0Anagrams
Explanation 0.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} CharacterFrequency: anagram
Frequency: marganaA
or a
33G
or g
11N
or n
11M
or m
11R
or r
11
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Sample Input 1anagrammmarganaa
Sample Output 1Not Anagrams
Explanation 1.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} CharacterFrequency: anagramm
Frequency: marganaaA
or a
34G
or g
11N
or n
11M
or m
21R
or r
11
The two strings don't contain the same number of a
's and m
's, so we print "Not Anagrams".
Sample Input 2.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} Hellohello
Sample Output 2Anagrams
Explanation 2.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} CharacterFrequency: Hello
Frequency: helloE
or e
11H
or h
11L
or l
22O
or o
11
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Solution in java8
Approach 1.
static boolean isAnagram(String a, String b) {
char[] s=a.toLowerCase().toCharArray();
char[] s1=b.toLowerCase().toCharArray();
Arrays.sort(s1);
Arrays.sort(s);
return Arrays.equals(s1,s);
}
Approach 2.
static boolean isAnagram(String a, String b) {
// Complete the function by writing your code here.
if (a.length() != b.length()) return false;
char[] ac = a.toLowerCase().toCharArray();
char[] bc = b.toLowerCase().toCharArray();
Arrays.sort(ac);
Arrays.sort(bc);
for (int i = 0; i < ac.length; ++i) {
if (ac[i] != bc[i]) {
return false;
}
}
return true;
}
Approach 3.
static boolean isAnagram(String a, String b) {
// Complete the function by writing your code here.
Boolean retValue = false;
String A = a;
String B = b;
if(A != null && B != null)
{
char [] arrayA = A.toLowerCase().toCharArray();
char [] arrayB = B.toLowerCase().toCharArray();
Arrays.sort(arrayA);
Arrays.sort(arrayB);
retValue = Arrays.equals(arrayA, arrayB);
}
return retValue;
}