Java Program to Find Whether the given words are Anagram or Not

5:59 PM 24 Comments A+ a-

Anagrams as you might already know, are the words or phrase formed by rearranging the letters of another word. For example: angel and glean or stain and satin.

Well, my description of what the anagrams are ends here as this is not an English grammar tutorial site. What I am more interested in writing out a piece of code to which we can pass the words and can get know whether the words were anagram or not. Well, this is what the solo purpose of this tutorial at least. May be you'd asked such a question somewhere someday, may be in some interview or some coding test, so I just thought to post this here. 

So, how do we proceed? Java is such a powerful programming language that using the provided library methods, you might be able to do this in no more than couple of lines of code. Great if you can! But having faced enough number of interviews to assure you of that, the interviewer in most cases, are not that interested in knowing how good you're at memorizing the method and class names but how quickly your brain can find out a solution to the given problem and your approach. So, the chances are that the Java programming interview questions that you'd be asked would expect you to solve the problem without relying much upon the library methods.
Well, we'll do it both ways and we'll just take two words as the input to our method. You can easily extend the code to include more. (var-args might be of some help!)

Using the library methods

This just makes it ridiculously easy! We will be just reading the  given strings, sorting them using Arrays.sort() and then comparing the sorted strings. If the sorted strings are equal then the words were anagrams. Simple! Because the anagrams as already stated are the words formed by rearranging the words in hand. 

Without using the library methods

Without using the library methods also does not make it any less simpler. We can just first read the two strings, check for their same lengths, take one string character by character and search the other for each characters. And if all characters are found from one string are found in another then the two strings are anagrams because they were of same length. 

So, enough of the text, now the program!

Java program to check whether the two strings are anagram or not

import java.util.Arrays;
import java.util.Scanner;

public class AnagramDemo {

 // method to check for the anagrams using built-in methods
 public static boolean isAnagram(String str1, String str2) {

  char[] charArr1 = str1.toCharArray();
  char[] charArr2 = str2.toCharArray();

  // performing sorting of string
  Arrays.sort(charArr1);
  Arrays.sort(charArr2);

  return Arrays.equals(charArr1, charArr2);
 }

 // method to check for the anagrams without using library methods
 public static boolean isAnagramManual(String str1, String str2) {
  // if length itself not equal, return false.
  if (str1.length() != str2.length())
   return false;
  // else do the brainstorming!
  boolean anagram = true;

  for (int i = 0; i < str1.length(); i++) {
   if (str2.indexOf(str1.charAt(i)) == -1)
    anagram = false;
  }

  return anagram;
 }

 public static void main(String[] args) {

  System.out.println("First string");
  Scanner sc = new Scanner(System.in);
  String str1 = sc.nextLine();
  System.out.println("Second string");
  String str2 = sc.nextLine();
  System.out.println("Using library methods:-");
  if (isAnagram(str1, str2))
   System.out.println(str1 + " and " + str2 + " are anagrams!");
  else
   System.out.println(str1 + " and " + str2 + " are NOT anagrams!");

  System.out.println("WITHOUT USING LIBRARY METHODS");
  if (isAnagramManual(str1, str2))
   System.out.println("ANAGRAMS");
  else
   System.out.println("NOT ANAGRAMS");

 }

}


I hope that you find it useful. In case you find any mistakes or the better way of doing it, please write in comments.
In case you have any other Java related question or problem that you want me to write code for, please let me know in comments or mail me