Java Program to Check if two strings are anagram



An anagram is a word or phrase formed by rearranging the letters of two or more words.

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 anagrams; otherwise, not.

Now, let's understand how we can find if two strings are anagrams or not in Java.

How to Check if two Strings are Anagram?

Follow the steps given below to solve the Anagram String problem:

  • First, convert the characters of the given Strings to lowercase.

  • Then, check whether the length of both strings is the same or not using an if-else block or any other approach of your choice. If it is not the same, then exit and print that both strings are not anagrams.

  • 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 or any other approach.

  • In the end, check whether both arrays contain the same characters or not using Arrays.equals() method or a for loop. If both arrays contain the same characters, then print strings are anagrams; otherwise not anagrams.

Example Scenario

Before implementing the above logic into the Java programs to check whether two strings are anagrams or not, let's discuss an example Scenario.

Input Strings:

String1 = "Heart";
String2 = "Earth";

Output:

Both strings are anagram

Explanation:

If we rearrange both strings, they will become "aehrt". Since both contain the same set of letters, they are anagrams.

Example 1: Using built-in Methods

The following example illustrates how to check if two strings are anagrams or not using Java built-in methods.

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();
      // checking length
      if(inputStr1.length() == inputStr2.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: Using for Loop

This is another Java program to check whether two strings are anagram or not. In this example, we will use for loop to sort the character array and to check if both array contains the same set of characters.

public class Main {
   // to sort the character array    
   public static void sArr(char[] cArray) {
      int n = cArray.length;
      for (int i = 0; i < n - 1; i++) {
         for (int j = 0; j < n - i - 1; j++) {
            if (cArray[j] > cArray[j + 1]) {
               // Swap elements
               char temp = cArray[j];
               cArray[j] = cArray[j + 1];
               cArray[j + 1] = temp;
            }
         }
      }
   }
   // to check anagram    
   public static boolean checkAnagram(String inputStr1, String inputStr2) {
      // converting their characters to lowercase
      inputStr1 = inputStr1.toLowerCase();
      inputStr2 = inputStr2.toLowerCase();
      // checking length
      if(inputStr1.length() == inputStr2.length()) { 
         // converting the given strings into character arrays
         char[] array1 = inputStr1.toCharArray();
         char[] array2 = inputStr2.toCharArray();
         // sorting both arrays
         sArr(array1);
         sArr(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;
      } else {
          return false;
      } 
   }
   public static void main(String[] args) {
      String inputStr1 = "Race";
      String inputStr2 = "Care";

      System.out.println("The given strings are: " + inputStr1 + " and " + inputStr2);
      
      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
Updated on: 2025-06-18T19:11:41+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements