How to check if two strings are anagrams in Java

In this shot, we will learn how to check whether or not two strings are anagrams in Java.

What are anagrams?

An anagram of a string is another string that has the same string length and elements, but may have the elements in a different order.

Let’s understand with the help of an example:

Suppose that the first string is “lamp” and the second string is “palm.” The second string is an anagram of the first string because only the arrangement of the letters is different; aside from that, the length and letters of the strings are the same.

Solution approach

  • First, we replace all the white spaces in the string with no space.

  • Now, we check if the lengths of the two strings are equal or not; if not, then the strings can’t be anagrams of each other.

  • If the lengths of the strings are equal, we convert the string into uppercase (we can also convert it to lowercase, as we want our anagrams to be case-insensitive) and convert it to a character array.

  • After that, we sort the array and check whether the two arrays are equal to analyze whether the strings are anagrams of each other or not.

We can use five methods in Java to solve this problem:

  1. Arrays.sort(): Accepts the array and sorts the elements of the array in increasing order.

  2. Arrays.equals(): Accepts two arrays and checks the equality of the arrays in terms of size, data, and order of elements.

  3. String.toUppercase(): Accepts a string and converts all the characters of the string into uppercase.

  4. String.replaceAll(): Accepts the regular expression (which needs to be replaced) and the sequence of characters that will be replaced, and then returns a string that replaces the sequence of characters matching regex and replacement string.

  5. toCharArray(): Accepts a string and converts that string into a character array.

Code

Let’s take a look at the code.

import java.util.*;
class Main
{
static void isAnagram(String str1, String str2)
{
String s1 = str1.replaceAll("\\s", "");
String s2 = str2.replaceAll("\\s", "");
boolean eq = true;
if (s1.length() != s2.length())
{
eq = false;
}
else
{
char[] arr1 = s1.toUpperCase().toCharArray();
char[] arr2 = s2.toUpperCase().toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
eq = Arrays.equals(arr1, arr2);
}
if (eq)
{
System.out.println(s1 + " and " + s2 + " are anagrams");
}
else
{
System.out.println(s1 + " and " + s2 + " are not anagrams");
}
}
public static void main(String[] args) {
isAnagram("Pat", "Tap");
isAnagram("Mother In Law", "Hitler Woman");
isAnagram("Ready","Steady");
}
}

Explanation

  • In line 1, we import the required package.

  • In line 2, we make a class Main.

  • In lines 4 to 33, we define a function to check whether the two strings are anagrams of each other or not.

  • In * lines 6 and 7*, we replace all the white spaces in the two given strings with the String.replaceAll() method.

  • In line 9, we assign true to a boolean type variable.

  • From lines 11 to 14, we check if the lengths of the two strings are equal, then assign false to that boolean type variable.

  • Else, in lines 15 to 24, we convert the two strings into uppercase and convert them to character arrays. Then, we sort the two arrays and check whether they are equal or not, and store the result in that boolean type variable.

  • In * lines 25 to 28*, we display the message with the result of being anagrams if the condition is true.

  • In lines 29 to 32, we display the message with the result of not being anagrams if the condition is false.

  • In line 35, we make a main() function.

  • In lines 36 to 38, we call the defined function and pass the two strings as arguments to check whether they are each other’s anagram or not.

Free Resources