title: How can check anagram strings in C#
body: <p>Given two strings A and B, check if they are anagrams.</p>
  
  <p><em>Two strings are said to be anagrams, if one string can be obtained by rearranging the letters of another.</em></p>
  
  <p>Examples of anagrams are </p>
  
  <ul>
  <li><code>dog, god</code> </li>
  <li><code>abac, baac</code></li>
  <li><code>123, 312</code></li>
  </ul>
  
  <p><code>abab, aaba</code> and <code>dab, baad</code> are not anagrams.</p>
  
  <p><strong>INPUT :</strong></p>
  
  <p>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;</p>
  
  <p><strong>OUTPUT</strong> </p>
  
  <p>For each test case, print "YES" if they are anagrams, otherwise print "NO". (without quotes)</p>
  
  <p><strong>Constraints</strong> </p>
  
  <ol>
  <li>1 &lt;= T &lt;= 10</li>
  <li>A and B both contain only lower case latin letters 'a' to 'z' and
  digits 0 to 9.</li>
  <li>Length of each string A and B does not exceed 5*10^5 (500000)</li>
  </ol>
  
  <p>`</p>
  
  <pre><code>Sample Input            Sample Output
  ------------------------------------
  3                           YES
  abcd bcda                   NO
  bad daa                     YES
  a1b2c3 abc123               NO
  </code></pre>
  
  <p>How can we do this ?</p>
  
  <pre><code>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&lt;char, int&gt; pool = new Dictionary&lt;char, int&gt;();
      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;
  }
  </code></pre>