
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Two Strings Are Anagram in Java
An anagram is a word or phrase formed by rearranging the letters of two or more words. Hence, the logic to check whether two given strings are anagram or not is as follows: Suppose there are two strings, if both strings contain the same set of letters along with the same number of letters regardless of their order, then we can say that both strings are anagram otherwise not. Now, let's understand how we can implement this logic in our Java programs to find if two strings are anagram or not.
Problem Statement
Before jumping to the Java programs to check whether two strings are anagram or not, let's discuss the problem statement first.
Instance
Input
String1 = "Heart"; String2 = "Earth";
Output
Both strings are anagram
Explanation
If we rearrange both strings, they will become "aehrt". Hence, they are anagram.
Now, let's discuss the above logic programmatically.
Approach to Check if two strings are anagram
First, initialize two arrays and convert their characters to lowercase.
Then, check whether the length of both strings is same or not using an if-else block. If it is the same then, enter the block otherwise exit and print both strings are not anagram.
If both strings are of the same length then, enter the if-else block. Inside this block, first, convert the strings into character arrays so that we can sort their characters.
Now, sort both arrays using the Arrays.sort() method.
In the end, check whether both arrays contain the same characters or not using Arrays.equals() method. If both arrays contain the same characters then, print strings are anagram otherwise not anagram.
Example 1
The following example illustrates how to check if two strings are anagram or not in Java.
import java.util.Arrays; public class Main { public static void main(String[] args) { // initializing two Strings String inputStr1 = "Heart"; String inputStr2 = "Earth"; System.out.println("The given strings are: " + inputStr1 +" and " + inputStr2); // converting their characters to lowercase inputStr1 = inputStr1.toLowerCase(); inputStr2 = inputStr2.toLowerCase(); // to check both strings are anagram or not if(inputStr1.length() == inputStr2.length()) { // checking length // converting the given strings into character arrays char[] array1 = inputStr1.toCharArray(); char[] array2 = inputStr2.toCharArray(); // sorting both arrays Arrays.sort(array1); Arrays.sort(array2); // checking equality and printing the result if(Arrays.equals(array1, array2)) { System.out.println("Both strings are anagram"); } else { System.out.println("Both strings are not anagram."); } } else { System.out.println("Both strings are not anagram."); } } }
Output
The given strings are: Heart and Earth Both strings are anagram
Example 2
This is another Java program to check whether two strings are anagram or not. In this example, we will create a user-defined method which will contain the logic for checking anagram.
import java.util.Arrays; public class Main { public static boolean checkAnagram(String inputStr1, String inputStr2) { // converting their characters to lowercase inputStr1 = inputStr1.toLowerCase(); inputStr2 = inputStr2.toLowerCase(); // to check both strings are anagram or not if(inputStr1.length() == inputStr2.length()) { // checking length // converting the given strings into character arrays char[] array1 = inputStr1.toCharArray(); char[] array2 = inputStr2.toCharArray(); // sorting both arrays Arrays.sort(array1); Arrays.sort(array2); // checking the characters are same or not for (int i = 0; i < array1.length; i++) { if ( array1[i] != array2[i]) { return false; } } } return true; } public static void main(String[] args) { // initializing two Strings String inputStr1 = "Race"; String inputStr2 = "Care"; System.out.println("The given strings are: " + inputStr1 +" and " + inputStr2); // calling the method to check anagram if(checkAnagram(inputStr1, inputStr2)) { System.out.println("Both strings are anagram"); } else { System.out.println("Both strings are not anagram."); } } }
Output
The given strings are: Race and Care Both strings are anagram
Conclusion
We started this article by defining anagram strings and explaining the logic to check if two strings are anagram or not. In the next section, we understood the logic in a more precise way with the help of an example. Later, we have written two Java programs to check whether two given strings are anagram or not.