title: Finding if two strings are anagrams in O(n) - solution using XOR
body: <p>I'm working on a problem from <a href="https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/" rel="nofollow noreferrer">hackerearth</a> </p>
  
  <p>The goal is to find if the input strings are anagrams in O(n) time.</p>
  
  <p>Input format:</p>
  
  <ul>
  <li>First line, contains an intger 'T' denoting no. of test cases. </li>
  <li>Each test consists of a single line, containing two space separated
  strings S1 and S2 of equal length.</li>
  </ul>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main()
  {
      int T;
      std::cin &gt;&gt; T;
      std::cin.ignore();
  
      for(int i = 0; i &lt; T; ++i)
      {
          std::string testString;
          std::getline(std::cin, testString);
  
          char test =  ' ';
  
          for (auto&amp; token : testString)
          {
              if(token != ' ')
                  test ^= token;
          }
  
          if (test == ' ')
              std::cout &lt;&lt; "YES\n";
          else
              std::cout &lt;&lt; "NO\n";
      }
  
  }
  </code></pre>
  
  <p>The code above fails 5/6 hackerearth tests.
  Where is my mistake? Is this a good approach to the problem?</p>