# Write a function to check whether two strings are anagram of each other.

Input: s – “abcde”, t – “abced”
Output = true

Input  s – “abcde”, t – “abcfed”
Output – false

Below are two solutions to this problem.

Method 1: Sort both the strings and then compare the strings. Time Complexity is O(nlogn).

```bool anagrams(string s, string t)
{
return s.sort() == t.sort()
}
```

Method 2: Check if the two strings have same count for each character.

1. Create an array of size 256 initialized with 0′s.
2. For first string increment count of character in count array.
3. For second string decrement the count of character from count array.
4. Repeat steps 2 and 3 till we reach end of any string.
5. Check if array contains only zero then strings are anagram otherwise not.

```bool anagrams(string s,string t)
{
if (s.length() ! = t.length())
return false;
int letters[256] = {0};
for(int i=0;i<s.length();i++)
{
letters[s[i]]++;
letters[t[i]]--;
}
for (int i = 0; i < 256; i++)
{
if(letters[i])
return false;
}
return true;
}
```

Time Complexity : O(n)

### 12 Thoughts on “Write a function to check whether two strings are anagram of each other.”

1. Naveen Gupta on July 6, 2013 at 5:14 pm said:

One more O(N) and Space complexity O(1) approach:

take XOR of both the strings and if output is zero , string are anagram to each other else not.

2. Amit Gaur on July 6, 2013 at 6:21 pm said:

Naveen your solution will not work.
lets take example

S1= “aaaa”;
S2= “bbbb”;

now apply your algo. here S1 and S2 are strings.

3. Naveen Gupta on July 6, 2013 at 7:28 pm said:

Amit

Here is the complete solution

```int check_anagram(char* str1, char* str2)
{
int l1 = strlen(str1);
int l2=strlen(str2);
if(l1 != l2)
return 0;

int val=0;
int sum1=0,sum2=0;
for(int i=0;i < l1; i++)
{
sum1 += str1[i];
sum2 += str2[i];
val ^= str1[i] ^ str2[i];
}

if((val == 0) && (sum1 == sum2))
return 1;
else
return 0;

return 0;
}
```
4. crazyadmin on July 6, 2013 at 10:29 pm said:

Sorry Naveen,

Your solution won’t work for input strings adad and bcbc. Here sum of both strings are same but they are not anagram.

5. Pingback: Find Anagrams in array of Strings

6. what If two string have different lenght?
Sample Input 1
tea
slate

Sample Output1
ate

will it work?

7. rdscomputersolutions.com on August 15, 2014 at 12:32 am said:

Do yoou mind iff I quote a few of your articles as long
as I provide credit and sources back to your site?
My blog site is in the very same area of interest as yours and my users would truly benefit frm a loot of the information you provide here.
Please let me know if this ok with you. Many thanks!

8. Tarun on June 27, 2015 at 7:16 pm said:

public class Anagram {
public static void main(String[] args) {
String s1=”asdfas” ;
String s2=”asdfas”;
char [] arr=new char[256];
System.out.println();
for(int i=0;i<s1.length();i++)
{
arr[s1.charAt(i)]++;
}
for(int i=0;i<s2.length();i++)
{
arr[s2.charAt(i)]–;

}
int sum=0;
for(int i=0;i<256;i++)
{
sum += arr[i];
if(arr[i]<0)
break;
}
if(sum==0)
System.out.println("true");

else
System.out.println("false");

}
}

9. Tarun on June 27, 2015 at 7:18 pm said:

public class Anagram 1{
public static void main(String[] args) {
String s1=”asdfas” ;
String s2=”asdfas”;
char [] arr=new char[256];
System.out.println();
for(int i=0;i<s1.length();i++)
{
arr[s1.charAt(i)]++;
}
for(int i=0;i<s2.length();i++)
{
arr[s2.charAt(i)]–;

}
int sum=0;
for(int i=0;i<256;i++)
{
sum += arr[i];
if(arr[i]<0)
break;
}
if(sum==0)
System.out.println("true");

else
System.out.println("false");

}
}

10. Karthik Venkatesh on July 14, 2015 at 8:33 pm said:

import java.util.*;

public class AnagramOrNot
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String s,t;
System.out.println(“Enter the first string”);
s = scan.nextLine();
System.out.println(“Enter the second string”);
t = scan.nextLine();

char[] chrs = s.toCharArray();
char[] chrt = t.toCharArray();
int count=0;

if(s.length()==t.length())
{
for(int i=0;i<s.length();i++)
{
for(int j=0;j<t.length();j++)
{
if(chrs[i]==chrt[j])
{
count++;
}
else
continue;
}
}
if(count==s.length() || count==t.length())
{
System.out.println("They are anagrams");
}
else
System.out.println("They are not anagrams");
}
else
System.out.println("The words lengths do not match so they are not anagrams");

}
}

11. Kevin F on March 20, 2016 at 8:49 am said:

using System;
public class AreAnagrams{
public static bool AreStringsAnagrams(string a, string b){
char[] s1 = a.ToCharArray();
char[] s2 = b.ToCharArray();
Array.Sort(s1);
Array.Sort(s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
string ss1 = new string(s1);
string ss2 = new string(s2);
if (ss1.CompareTo(ss2) == 0)
return true;
return false;
}

public static void Main(string[] args){
Console.WriteLine(AreStringsAnagrams(“asdfg”, “qwerty”));