Summary: In this programming example, we will learn multiple ways to check anagram strings in Java.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Wikipedia
Example:
stop <-> pots cat <-> tac hello <-> lleho
Method 1: Using substring()
The idea is to one by one pick a character from string-I and remove the same from string-II.
At the end of the process, if the length of the string-II successfully reduces to 0 then the given two strings are anagram, otherwise not.
To remove the characters from string-II, we use the apply substring() method on string-II.
class Main {
public static void main(String[] args) {
if(checkAnagram("stop", "pots"))
System.out.println("Anagram");
else
System.out.println("Not an Anagram");
}
//Function checks anagram using the substring() method
public static boolean checkAnagram(String a,String b){
//if lengtha sre not identical then strings are not Anagram
if(a.length() != b.length())
return false;
/*
*pick characters from string-I one by one
*and remove the same from String-II
*/
int index;
for(int i=0; i<a.length(); i++){
//if a character of string-I doesn't even exixit in string-II
//then the strings are not anagram
if((index = b.indexOf(a.charAt(i))) != -1){
//removing character using the substring() method
b= b.substring(0, index) + b.substring(index+1, b.length());
} else{
return false;
}
}
//If length of string-II reduces to 0, then strings are Anagram
if(b.length() == 0)
return true;
else
return false;
}
}
Output:
Anagram
Note: If any character of string-I doesn’t appear in string-II, then they are not anagrams.
Method 2: Using StringBuffer class
In this method, we use the same logic as the above program but instead of the substring()
method, we use the StringBuffer
class to remove characters from the second string.
class Main {
public static void main(String[] args) {
if(checkAnagram("cat", "tom"))
System.out.println("Anagram");
else
System.out.println("Not an Anagram");
}
//Function check anagram using StringBuffer
public static boolean checkAnagram(String a, String b){
//if lengths are not identical then strings are not Anagram
if(a.length() != b.length())
return false;
StringBuffer sb = new StringBuffer(b);
/*
*pick characters from string-I one by one
*and remove the same from String-II
*/
for(int i=0; i<a.length(); i++){
sb.deleteCharAt(sb.indexOf(a.charAt(i)+""));
}
//If length of string-II reduces to 0, then strings are Anagram
if(sb.length() == 0)
return true;
else
return false;
}
}
Output:
Not an Anagram
Method 3: Using Char Array
In this method, we transform strings into char arrays and check their similarity using the Arrays.equals()
method after sorting them in ascending order using the Arrays.sort()
method.
If both the arrays turn out to be equal then they are Anagram, otherwise not.
String1 = pots String2 = stop //Before Sorting CharArray1 = ['p','o','t','s'] CharArray2 = ['s','t','o','p'] //After Sorting CharArray1 = ['o','p','s','t'] CharArray2 = ['o','p','s','t'] CharArray1 == CharArray2 (TRUE)
Here is the implementation of the idea in Java:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
if(checkAnagram("mot", "tom"))
System.out.println("Anagram");
else
System.out.println("Not an Anagram");
}
//Function checks Anagram after changing string to char array
public static boolean checkAnagram(String a, String b){
char aArray[] = a.toCharArray();
char bArray[] = b.toCharArray();
Arrays.sort(aArray);
Arrays.sort(bArray);
return Arrays.equals(aArray,bArray);
}
}
Output:
Anagram
These were the three ways using which we can check whether the two strings are Anagram of each other or not.