Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Java Anagrams problem solution

YASH PAL, 31 July 2024

In this HackerRank Java Anagrams problem in the java programming language, 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.

HackerRank Java Anagrams problem solution

HackerRank Java Anagrams problem solution.

import java.util.Scanner;

public class Solution {

    static boolean isAnagram(String s1, String s2) {
        // Complete the function
        s1=s1.toLowerCase();
        s2=s2.toLowerCase();
        
        if(s1.length()==s2.length())

        {
            int[] a = new int[256];
            int[] b = new int[256];
            for (int i = 0; i < s1.length(); i++) {
                a[(int) s1.charAt(i)] += 1;
                b[(int) s2.charAt(i)] += 1;
            }
            for (int i = 0; i < 256; i++) {
                if (a[i] != b[i])
                    return false;

            }
            return true;
        }
        else
        {
            return false;
        }
    }

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

Second solution

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

public class Solution {

   static boolean isAnagram(String A, String B) {
      
       char[] arrA = A.toUpperCase().toCharArray();
       char[] arrB = B.toUpperCase().toCharArray();
       Arrays.sort(arrA);
       Arrays.sort(arrB);
       
       for(int i = 0; i < A.length(); i++)
           try {
               if(Character.toUpperCase(arrA[i]) != Character.toUpperCase(arrB[i])) return false;
           } // end try
           catch(Exception e) {
               return false;
           } // end catch
       return true;
   } // end isAnagram
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        boolean ret=isAnagram(A,B);
        if(ret)System.out.println("Anagrams");
        else System.out.println("Not Anagrams");
        
    } // end main
} // end class

A solution in java8 programming.

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

public class Solution {

   static boolean isAnagram(String A, String B) {
       if(A == null || B == null) {
           if(A != null || B != null) {
               return false;
           }
           return true;
       }
       A = A.toLowerCase();
       B = B.toLowerCase();
       char[] aArr = A.toCharArray();
       char[] bArr = B.toCharArray();
       Arrays.sort(aArr);
       Arrays.sort(bArr);
       String aSorted = new String(aArr);
       String bSorted = new String(bArr);
       
       return aSorted.equals(bSorted); 
       
       
   
   }
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        boolean ret=isAnagram(A,B);
        if(ret)System.out.println("Anagrams");
        else System.out.println("Not Anagrams");
        
    }
}
coding problems solutions Hackerrank Problems Solutions Java Programming Tutorials

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes