How to check if two String are Anagram in Java - Program Example (original) (raw)

Write a Java program to check if two String is an anagram of each other, is another good coding question asked at fresher level Java Interviews. This question is on a similar level of finding the middle element of LinkedList in one pass and swapping two numbers without using the temp variable. By the way, two String is called anagram, if they contain the same characters but on different order e.g. army and mary, stop and pots, etc. Anagrams are actually a mix-up of characters in String. If you are familiar with String API, i.e. java.lang.String then you can easily solve this problem.

In order to check if Strings are anagrams, you need to get their character array and see if they are equal or not. Though you can also use indexOf(), substring(), and StringBuffer or StringBuilder class to solve this question.

In this Java program, we will see 3 ways to solve these interview questions, and check if two String are anagrams or not. By the way, if you are preparing for a Java interview, it's good to prepare some data structures and algorithms questions as well. More often, there is one or more questions from programming, coding, and logic in these interviews.

Java program to check if String is an anagram

String Anagram Check - 3 ways to find if two Strings are anagrams or notAs I said, there are multiple ways to find if two strings are anagrams or not. The classical way is getting a character array of each String, and then comparing them, if both char array is equal then Strings are anagram.

But before comparing, make sure that both Strings are in the same case e.g. lowercase or uppercase and character arrays are sorted because equals method of Arrays, return true, only if the array contains the same length, and each index has the same character.

For simplicity, I have left checking if String is null or empty and converting them into uppercase or lowercase, you can do that if you want. If the Interviewer asks you to write production quality code, then I would suggest definitely put those checks and throw IllegalArgumentException for null String or you can simply return false.

I would personally prefer to return false rather than throwing Exception, similar to the equals() method. Anyway, here are three ways to check if two String are Anagram or not. I have also included a JUnit Test to verify various String which contains both anagram and not.

import java.util.Arrays;

/**

}

JUnit Test Case for String Anagram Example

here is our JUnit tests for all three 3 methods of AnagramCheck class, we have actually tested all method with similar set of input.

import org.junit.Test; import static org.junit.Assert.*;

/**

}

Output Testsuite: StringAnagramTest Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.094 sec

Our AnagramCheck class contains 3 static methods to verify if Strings are anagram or not. First one, takes character array of first String and loop through it, then finds that character in second String, and deletes it by using substring method. If second String doesn't contains a character than the method return false immediately.

At the end of the test if the second String is empty than both Strings are anagram because they contain the same set of characters. To improve performance, we have checked length at the very start of this method, as two String with different lengths can not be anagram of each other.

The third method is exactly same of first one, except, it uses deleteCharAt(int index) method of StringBuilder for deleting characters.

That's all on how to check if two String are anagrams of each other. Don't forget to check other popular programming and coding questions from various Java interviews, mentioned below :

And lastly one question for you, which coding exercise if your favorite in Java? Fibonacci, Palindrome, Permutation, Prime number or this one?