Given two strings A and B, check if they are anagrams.
Two strings are said to be anagrams, if one string can be obtained by rearranging the letters of another.
Examples of anagrams are
dog, god
abac, baac
123, 312
abab, aaba
and dab, baad
are not anagrams.
INPUT :
First line of the input is the number of test cases T. It is followed by T lines, each line has two space separated strings A and B;
OUTPUT
For each test case, print "YES" if they are anagrams, otherwise print "NO". (without quotes)
Constraints
`
Sample Input Sample Output
------------------------------------
3 YES
abcd bcda NO
bad daa YES
a1b2c3 abc123 NO
How can we do this ?
bool anagramChecker(string first, string second)
{
if(first.Length != second.Length)
return false;
if(first == second)
return true;//or false: Don't know whether a string counts as an anagram of itself
Dictionary<char, int> pool = new Dictionary<char, int>();
foreach(char element in first.ToCharArray()) //fill the dictionary with that available chars and count them up
{
if(pool.ContainsKey(element))
pool[element]++;
else
pool.Add(element, 1);
}
foreach(char element in second.ToCharArray()) //take them out again
{
if(!pool.ContainsKey(element)) //if a char isn't there at all; we're out
return false;
if(--pool[element] == 0) //if a count is less than zero after decrement; we're out
pool.Remove(element);
}
return pool.Count == 0;
}
Anagram strings are nothing but, all the characters that occur for the same number of times in another string, which we call as anagrams. A user enters two strings. We need to count how many times each letter ('a' to 'z') appears in them and then, compare their corresponding counts.
Two words are anagrams of each other if they contain the same number of characters and the same characters. You should only need to sort the characters in lexicographic order, and determine if all the characters in one string are equal to and in the same order as all of the characters in the other string.
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.
In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.
Is this your solution?
string a = "abcd";
string b = "bcda"; // bad daa a1b2c3 abc123
string aa = String.Concat(a.OrderBy(c => c));
string bb = String.Concat(b.OrderBy(c => c));
if (aa == bb)
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
or shorter
if (String.Concat(a.OrderBy(c => c)).Equals(String.Concat(b.OrderBy(c => c))) ...
There's the fast way and the simple way:
void Test()
{
string a = "abccabccabccabccabccabccabccabccabccabccabccabccabccabccabccabcc";
string b = "bcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcac";
IsAnagramSimple(a, b);
IsAnagramFast(a, b);
}
private bool IsAnagramSimple(string a, string b)
{
return a.OrderBy(c => c).SequenceEqual(b.OrderBy(c => c));
}
private bool IsAnagramFast(string a, string b)
{
if (a.Length != b.Length)
{
return false;
}
var aFrequency = CalculateFrequency(a);
var bFrequency = CalculateFrequency(b);
foreach (var key in aFrequency.Keys)
{
if (!bFrequency.ContainsKey(key)) return false;
if (aFrequency[key] != bFrequency[key]) return false;
}
return true;
}
private Dictionary<char, int> CalculateFrequency(string input)
{
var frequency = new Dictionary<char, int>();
foreach (var c in input)
{
if (!frequency.ContainsKey(c))
{
frequency.Add(c, 0);
}
++frequency[c];
}
return frequency;
}
public bool IsAnagram(string s1,string s2)
{
if (s1.Length != s2.Length)
return false;
var s1Array = s1.ToLower().ToCharArray();
var s2Array = s2.ToLower().ToCharArray();
Array.Sort(s1Array);
Array.Sort(s2Array);
s1 = new string(s1Array);
s2 = new string(s2Array);
return s1 == s2;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With