What is anagram in Java?
In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java programme question is regularly asked.
Anagram
A phrase or a word that has been created by reversing the letters of that other phrase or a word is known as an anagram, or Anagrams are words or phrases created by rearranging the letters, according to the dictionary definition of the term.
If rearranging or mixing up the letters in two strings results in a meaningful term, then the corresponding strings are referred to as anagrams. In other words, if two strings include the same characters in a different order, we can say that they are anagrams. Remember that a letter only needs to be used once.
Let us have a look at some of the examples related to the anagram.
Example of Anagram
There are many anagrams of words, some of which include:
Moon starer = astronomer
Shrub = brush
Store scum = customers
Indicatory = dictionary
Dormitory = dirty room
Editor = redo it
Eleven plus two = twelve plus one
How can two strings be tested to see if they are anagrams?
- Read or create the strings sr1 and sr2 from scratch.
- Calculate the lengths of both strings.
- Compare the strings lengths.
- Printing strings are not anagrams if the lengths are not identical.
- If not, take the next action.
- The string should be changed into something like a character array.
- Use the sort() method to sort both arrays.
- Use the equals() method to compare the strings after sorting. The equals() method returns a Bool variable that should be used to store the value.
- In the if statement, pass the variable. The following strings are anagrams if the test returns true. Not an anagram is else.
Let's include the following activities into a Java programme.
Program for the Java Anagram
There are many ways to find an anagram string, however in this part we'll concentrate on the three methods listed below.
By Using the for Loop
Let the program file name be AnagramDemo.java
public class AnagramDemo
{
//function checks if the strings are anagram or not
public static boolean theAnagram(String sr7, String sr9)
{
//comparing of the length
if (sr7.length() != sr9.length())
{
// if the strings' lengths are not equal, returns incorrect.
return false;
}
// string to character array conversion
char[] characs = sr1.toCharArray();
// loop cycles across the array.
for (char s : characs)
{
//finds the index
int index = sr9.indexOf(s);
if (index != -1)
{
//the substring() method returns a new string that is a substring of this string
Sr9 = sr9.substring(0, index) + sr9.substring(index + 1, sr9.length());
}
else
{
return false;
}
}
return sr9.isEmpty();
}
//It is the driver code
public static void main(String args[])
{
//we are calling the function
System.out.println(theAnagram("SHRUB", "BRUSH"));
System.out.println(theAnagram("EAT", "TEA"));
System.out.println(theAnagram("KAMAL", "MALAK"));
}
}
Output:

By Using the StringBuilder Class
Let the program file name be AnagramDemo2.java
public class AnagramDemo3
{
// determines whether the strings are anagrams or not.
public static boolean theAnagram(String sr7, String sr9)
{
// string to character array conversion
char[] charac = sr7.toCharArray();
StringBuilder sa = new StringBuilder(sr9);
// iterates through the character array in a loop.
for (char cr : charac)
{
// locates the char's index
int index = sa.indexOf("" + cr);
if (index != -1)
{
// at the specified index, remove the character
sa.deleteCharAt(index);
}
else
{
return false;
}
}
//It finds the length
return sa.length() == 0 ? true : false;
}
//It is the driver code
public static void main(String args[])
{
//It is calling the function
System.out.println(isAnagram("TON", "NOT"));
System.out.println(isAnagram("CAM", "MIC"));
}
}
Output:

By Using the Arrays Class
The equals() method and the String.sort() function are used in the programme below to determine whether or not the two texts are anagrams.
Let the program file name be AnagramDemo3.java
import java.util.Arrays;
public class AnagramDemo3
{
// function that determines whether or not the strings are anagrams
static void isAnagram(String sr7, String sr9)
{
// eliminates the white spaces in string 7
String sr7 = sr7.replaceAll("\\sr", "");
//removes white spaces from string 2
String sr9 = sr9.replaceAll("\\sr", "");
boolean condition = true;
// determines whether the lengths of the two strings are equal
if (sr7.length() != sr9.length())
{
// If the length of the strings differs, condition is false.
condition = false;
}
// executes if the string lengths are equal
else
{
// the string 7 is changed to lower case before being changed into a character array.
// arraySr7 saves the final string.
char[] arraySr7 = sr7.toLowerCase().toCharArray();
// lowercases the number 9 in the string before turning it into a character array.
//fi arraySr9 contains the final string.
char[] arraySr9 = sr9.toLowerCase().toCharArray();
// arraySr7's character set is sorted.
Arrays.sort(arraySr7);
// arraySr9's character set is sorted.
Arrays.sort(arraySr9);
//it will compar the strings
status = Arrays.equals(arraySr7, arraySr9);
}
if (condition)
{
// if status is true, prints
System.out.println(sr7 + " and " + sr9 + " are the anagrams");
}
else
{
//prints if status returns false
System.out.println(sr7 + " and " + sr9 + " are not the anagrams");
}
}
//It is the driver code
public static void main(String args[])
{
//we are calling function
theAnagram("TEA", "ATE");
theAnagram("BAT", "TAB");
theAnagram("COOL", "LOOK");
}
}
Output:
