Join our newsletter for the latest updates.
Java Program to Check if two strings are anagram

Java Program to Check if two strings are anagram

In this example, we will check if two strings are anagram or not in Java.

To understand this example, you should have the knowledge of the following Java programming topics:


Two strings are said to be anagram if we can form one string by arranging the characters of another string. For example, Race and Care. Here, we can form Race by arranging the characters of Care.

Example 1: Java program to check if two strings are anagrams

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    String str1 = "java";
    String str2 = "vaaj";

    // check if length is same
    if(str1.length() == str2.length()) {

      // convert strings to char array
      char[] charArray1 = str1.toCharArray();
      char[] charArray2 = str2.toCharArray();

      // sort the char array
      Arrays.sort(charArray1);
      Arrays.sort(charArray2);

      // if sorted char arrays are same
      // then the string is anagram
      boolean result = Arrays.equals(charArray1, charArray2);

      if(result) {
        System.out.println(str1 + " and " + str2 + " are anagram.");
      }
      else {
        System.out.println(str1 + " and " + str2 + " are not anagram.");
      }
    }
    else {
      System.out.println(str1 + " and " + str2 + " are not anagram.");
    }
  }
}

Output

Java and Vaaj are anagram.

In Java, we have two strings named str1 and str2. Here, we are checking if str1 and str2 are anagrams.

Here,

  • str1.toCharArray() - converts the string into a char array
  • Arrays.sort() - sorts both the char arrays
  • Arrays.equal() - checks if the sorted char array are equal

If sorted arrays are equal, then the strings are anagram.

Note: Here, the example is case-sensitive. That is, Java and java are not anagrams. It is because the Arrays.sort() compares two characters with ASCII value. And, the ASCII value of J and j are different.


Example 2: Take string inputs from users and check if the strings are anagram

import java.util.Arrays;
import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // take input from users
    System.out.print("Enter first String: ");
    String str1 = input.nextLine();
    System.out.print("Enter second String: ");
    String str2 = input.nextLine();

    // check if length is same
    if(str1.length() == str2.length()) {

      // convert strings to char array
      char[] charArray1 = str1.toCharArray();
      char[] charArray2 = str2.toCharArray();

      // sort the char array
      Arrays.sort(charArray1);
      Arrays.sort(charArray2);

      // if sorted char arrays are same
      // then the string is anagram
      boolean result = Arrays.equals(charArray1, charArray2);

      if(result) {
        System.out.println(str1 + " and " + str2 + " are anagram.");
      }
      else {
        System.out.println(str1 + " and " + str2 + " are not anagram.");
      }
    }
    else {
      System.out.println(str1 + " and " + str2 + " are not anagram.");
    }

    input.close();
  }
}

Output

Enter first String: Race
Enter second String: Care
Race and Care are anagram.

In the above example, we have used the Scanner class to take input from the user. Here, we checked if the strings provided by users are anagram.