Java Program to Check if two strings are anagram

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

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 = "Race";
    String str2 = "Care";
    
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();

    // 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

race and care are anagram.

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

We first convert the strings to lowercase. It is because Java is case sensitive and R and r are two difference characters in Java.

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: The Arrays.sort() compares two characters with ASCII value. And, characters R and r are not equal. Hence, strings should be converted to the same case.


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.

Leave a Reply