Write a java program to check if two strings are anagrams of each other

What is an anagram string and how to write a Java program to check if two strings are anagrams of each other? Let’s first understand what are anagrams.

Anagrams:

Two strings are said to be anagrams of each other if they contain the same characters but in a different order.

It means that the characters and the no. of occurrences of the characters in the first string are the same as that of the second string only the arrangement of characters is different.

Example:

“silent” and “listen” are anagrams of each other as both contain the same characters.

Write an algorithm to check if two strings are anagrams of each other:

  1. First, we will take two strings as input from the user.
  2. For primary check, we will check if the lengths of the two strings are equal or not. If the lengths are not equal, the strings are not anagrams of each other. Otherwise, we will move to step 3.
  3. We will create a count array of size 26 that is by default initialized to 0. It contains the no. of occurrences of all the alphabets.
  4. Initially, the count array is used to hold the no. of occurrences of the first string. This is done by incrementing the count array by the occurrences of characters of the first string.
  5. Then, the same array will be used to hold the no. of occurrences of the second string but this time by decreasing the count array by the no. of occurrences of characters of the second string.
  6. For this, we will create a for loop which runs from i=0 to i=n1-1. It first increments the count array for the first string and then decrements for the second string. As the ASCII value of  ‘a’ is 97, we will decrease the index by 97. So that 0th index represents the occurrence of ‘a’, 1st  index of ‘b’, 2nd index of ‘c’ and so on till 25th index of ‘z’.
  7. If the count array has got all the elements as 0, the strings are anagrams of each other, else they are not.

Program to check if the two strings are anagrams of each other:

import java.util.Scanner;
public class AnagramString 
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter first string:");
        String s1=sc.next();
        System.out.println("Enter second string:");
        String s2=sc.next();
        int n1=s1.length();
        int n2=s2.length();
        if(n1!=n2) System.out.println("The strings are not anagrams of each other");
        else
        {
            int count[]=new int[26];
            for(int i=0;i<n1;i++)
            {
                count[s1.charAt(i)-97]++;
                count[s2.charAt(i)-97]--;
            }
            boolean flag=true;
            for(int i=0;i<26;i++)
            {
                if(count[i]!=0)
                {
                    flag=false;
                    break;
                }
            }
            if(flag) System.out.println("The two strings are anagrams of each other");
            else System.out.println("The two strings are not anagrams of each other");
        }
    }
}

Output of the program to check if the two strings are anagrams of each other: