Jul 24, 2021
Check whether two strings are anagram of each other
An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.
import java.util.Arrays;
class Anagram
{
public static void main(String args[])
{
char str1[] = { 't', 'e', 's', 't' };
char str2[] = { 't', 't', 'e', 's' };
int n1 = str1.length;
int n2 = str2.length;
if(n1 != n2)
System.out.println("Not a Anogram");
else
{
Arrays.sort(str1);
Arrays.sort(str2);
Boolean flag = true;
for(int i=0;i<n1;i++)
{
if(str1[i] != str2[i])
{
flag =false;
break;
}
}
if(flag == true)
System.out.println("Its a Anogram");
else
System.out.println("Not a Anogram");
}
}
}