Hackerrank Java Anagrams Solution
2 min read

Hackerrank Java Anagrams Solution

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: anagramFrequency: marganaA or a33G or g11N or n11M or m11R or r11

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: anagrammFrequency: marganaaA or a34G or g11N or n11M or m21R or r11

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: HelloFrequency: helloE or e11H or h11L or l22O or o11

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;
        
        
    }

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×