title: How to create a unique hash that will match any strings permutations
body: <p>Given a string <code>abcd</code> how can I create a unique hashing method that will hash those 4 characters to match <code>bcad</code> or any other permutation of the letters <code>abcd</code>?</p>
  
  <p>Currently I have this code</p>
  
  <pre><code>long hashString(string a) {
      long hashed = 0;
      for(int i = 0; i &lt; a.length(); i++) { 
          hashed += a[i] * 7; // Timed by a prime to make the hash more unique?
      } 
      return hashed;
  }
  </code></pre>
  
  <p>Now this will not work becasue <code>ad</code> will hash with <code>bc</code>. </p>
  
  <p>I know you can make it more unique by multiplying the position of the letter by the letter itself <code>hashed += a[i] * i</code> but then the string will not hash to its permutations.</p>
  
  <p><strong>Is it possible to create a hash that achieves this?</strong></p>
  
  <p><strong>Edit</strong></p>
  
  <p>Some have suggested sorting the strings before you hash them. Which is a valid answer but the sorting would take O(nlog) time and I am looking for a hash function that runs in O(n) time.</p>
  
  <p>I am looking to do this in O(1) memory.</p>