Check whether two strings are anagram of each other ?
Check whether two strings are anagram of each other ?
- Write a function to check whether two given strings are anagram of each other or not.
- An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “state” and “taste” are anagram of each other.
Read Also
Sample code in Java
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
class GFG {
/* function to check whether two strings are
anagram of each other */
static boolean areAnagram(char[] str1, char[] str2)
{
// Get lenghts of both strings
int n1 = str1.length;
int n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return false;
// Sort both strings
Arrays.sort(str1);
Arrays.sort(str2);
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
/* Driver program to test to print printDups*/
public static void main(String args[])
{
char str1[] = { 's', 't', 'a', 't' ,'e'};
char str2[] = { 't', 'a', 's', 't' , 'e' };
if (areAnagram(str1, str2))
System.out.println("The two strings are" + " anagram of each other");
else
System.out.println("The two strings are not" + " anagram of each other");
}
}
Output
The two strings are anagram of each other