Java Program to Check if two strings are anagram

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to check if two strings are anagram. An anagram is a word or phrase formed by rearranging the letters of a different word. A user enters two strings. We need to count how many times each letter ('a' to 'z') appears in them and then, compare their corresponding counts. The frequency of an alphabet in a string is how many times it appears in it. If two strings have same count of the frequency of particular alphabet then, we can say those two strings are anagrams.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the first string : Race
Enter the second string : Care

Output

The desired output would be −

The strings race and care are anagram.

Algorithm

Step 1 - START
Step 2 - Declare two string values namely my_string_1, my_string_2
Step 3 - Read the required values from the user/ define the values
Step 4 - Convert both the strings to lower case letters using toLowerCase() function
Step 5 - Check if the length of the two strings are same, if not, they are not anagram strings.
Step 6 - Assign the strings to character arrays and sort them.
Step 7 - Use the function ‘equals()’ to check if they are equal. If yes, they are anagram strings, else they are not anagram strings.
Step 8 - Display the result
Step 9 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool run button.

import java.util.Scanner;
import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String my_string_1, my_string_2;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the first string : ");
      my_string_1 = my_scanner.nextLine();
      System.out.print("Enter the second string : ");
      my_string_2 = my_scanner.nextLine();
      my_string_1 = my_string_1.toLowerCase();
      my_string_2 = my_string_2.toLowerCase();
      if(my_string_1.length() == my_string_2.length()) {
         char[] my_array_1 = my_string_1.toCharArray();
         char[] my_array_2 = my_string_2.toCharArray();
         Arrays.sort(my_array_1);
         Arrays.sort(my_array_2);
         boolean my_result = Arrays.equals(my_array_1, my_array_2);
         if(my_result) {
            System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are anagram.");
         } else {
            System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
         }
      } else {
         System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the first string : Race
Enter the second string : Care
The strings race and care are anagram.

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.util.Arrays;
   public class Main {
      public static void main(String[] args) {
         System.out.println("Required packages have been imported");
         String my_string_1, my_string_2;
         my_string_1 = "Race";
         my_string_2 = "Care";
         System.out.println("The two strings are defined as " +my_string_1 +" and " + my_string_2);
         my_string_1 = my_string_1.toLowerCase();
         my_string_2 = my_string_2.toLowerCase();
         if(my_string_1.length() == my_string_2.length()) {
            char[] my_array_1 = my_string_1.toCharArray();
            char[] my_array_2 = my_string_2.toCharArray();
            Arrays.sort(my_array_1);
            Arrays.sort(my_array_2);
            boolean my_result = Arrays.equals(my_array_1, my_array_2);
            if(my_result) {
               System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are anagram.");
            } else {
               System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
            }
         } else {
         System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
      }
   }
}

Output

Required packages have been imported
The two strings are defined as Race and Care
The strings race and care are anagram.
raja
Updated on 22-Feb-2022 12:09:59

Advertisements