Java Anagrams - HackerRank Solution

Java Anagrams - HackerRank Solution

Hello Friends, How are you? Today I am going to share the solution of Hackerrank Java Anagrams Problems. This is a very important problem of the Hackerrank java domain. In this post, I'll give two approaches to solve HackerRank Java Anagrams Problem. If you want only the solution then jump directly to the code and learn.

{tocify} $title={Table of Contents}

Java Anagrams - Hacker Rank Solution

What are Anagrams?

An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.

Note: If you want more information on Java Anagrams then please visit Java Anagrams on GeeksforGeeks.

Java Anagrams HackerRank Problem Statement

Two strings, a and b, 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.

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.

Constraints

  • 1 <= length(a), length(b) <= 50
  • Strings a and b consist of English alphabetic characters.
  • The comparison should NOT be case sensitive.

Sample Input 0

anagram margana {codeBox}

Sample Output 0

Anagrams {codeBox}

Explanation 0

CharacterFrequency: anagramFrequency: margana
A or a33
G or g11
N or n11
M or m11
R or r11

The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

Sample Input 1

anagramm marganaa {codeBox}

Sample Output 1

Not Anagrams {codeBox}

Explanation 1

CharacterFrequency: anagramFrequency: marganaa
A or a34
G or g11
N or n11
M or m21
R 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

Hello hello {codeBox}

Sample Output 2

Anagrams {codeBox}

Explanation 2

CharacterFrequency: HelloFrequency: hello
E or e11
H or h11
L or l22
O or o11

The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

HackerRank Java Anagrams Solution

Approach I:

import java.util.Scanner;

public class Solution {

    static boolean isAnagram(String a, String b) {
        a=a.toUpperCase();
        b=b.toUpperCase();
        boolean ret = false;
        StringBuilder c= new StringBuilder(b);

        if(a.length()==b.length()){
            for(int i=0; i<a.length();i++){
                for(int j=0; j<c.length();j++){
                    if(a.charAt(i)==c.charAt(j)){
                            c.deleteCharAt(j);
                        if(i==a.length()-1 && c.length()==0){
                            ret=true;
                            break;
                        }
                        break;

                    }

                }
            }
        }return ret;
   
   }

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


Approach II:

import java.io.*;
import java.util.*;

public class Solution {
    static boolean isAnagram(String a, String b) {
        if ((a == null || b == null) || (a.length() != b.length())) {
            return false;
        }
        final char[] ARRAY_A = a.toUpperCase().toCharArray();
        final Map map = new HashMap<>();
        for (int i = 0; i < ARRAY_A.length; i ++) {
            if (map.containsKey(ARRAY_A[i])) {
                map.put(ARRAY_A[i], (map.get(ARRAY_A[i]) + 1));
            } else {
                map.put(ARRAY_A[i], 1);
            }
        }
        final char[] ARRAY_B = b.toUpperCase().toCharArray();
        for (int i = 0; i < ARRAY_A.length; i ++) {
            if (map.containsKey(ARRAY_B[i])) {
                Integer count = map.get(ARRAY_B[i]);
                if (count == 0) {
                    return false;
                } else {
                    count --;
                    map.put(ARRAY_B[i], count);
                }
            } else {
                return false;
            }
        }
        return true;
    }
    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" );
    }
}

More Solutions




Disclaimer: The above Problem (Java Anagrams) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these two solutions will pass all the test cases. Now visit Java Anagrams HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post