- Compute the sums s1, s2, ..., sn of each of the n possible input symbols in your string
- Consider the unary (base 1) representations thereof: u1, u2, ..., un. We'll take the unary representation of a number n to be the string 0^n.
- Construct a binary string hash = (u1)1(u2)1...1(un)
- Interpret hash as the binary representation of a number of string and use a hash with your properties
If x and y have the same number of symbol k, then uk will be the same; and if this is true for all 1 <= k <= n then the hashes must be the same. It is also easy to show that strings that have different counts will have different hashes.
How big are these hashes? When the total number of symbols in the input is zero, the output is 1^n of length n bits. When you add a symbol to the input, one count increases by one, one unary representation increases in length by 1, and the overall size of the hash increases by one bit. So an input string of length m will have length m+n bits.
This algorithm is O(m+n) time and space where n is the fixed number of input symbols and m is the number of elements in the input string.
EDIT: An example would help.
Say that the input alphabet is A = {a, b, c, d, e}. We impose an ordering on the set to get the sequence L = (a, b, c, d, e). So L(0) = a, L(1) = b, etc. Now we want to hash the following two strings: abcd and bcda.
We record the partial sums in unary (note that the result is the same for both strings):
u1 = 0
u2 = 0
u3 = 0
u4 = 0
u5 = (empty)
We construct the hash as follows: (u1)1(u2)1(u3)1(u4)1(u5)1 = 010101011.
So the hash of both strings is equal to 171 when the output of the above procedure is interpreted as the binary encoding of an integer. If interpreted as a string over the input alphabet of size 5, 171 = 1*125 + 1*25 + 4*5 + 1*1 and thus we can encode it as bbeb. To be explicit, we are considering the input alphabet to be digits of a base-5 encoding of numbers where the digit x is equal to x's place in the ordering of the alphabet. So a = 0, b = 1, c = 2, d = 3, e = 4. The number bbeb is therefore equal to 1*5^3 + 1*5^2 + 4*5^1 + 1*5^0 = 125 + 25 + 20 + 1 = 171.
One might object that this is going to produce a hash that is similar in length to the input. If you want one that's fixed length, just run a hash on the output of this one that has the properties you want. Then the composition will be a hash function that you like.