0

All input will be lowercase English alphabet.

HashString("ab")= should be unique value
HashString("ba")= should give me the same value as above

I tried with assigning each alphabet with a number, but it turned out to be wrong logic

My attempt produced following output.

HashString("ab")=3
HashString("ba")=3 this is correct.
HashString("c")=3  this is wrong.
8
  • 1
    What is the longest string which can be input? Commented Jul 12, 2017 at 11:14
  • 1
    You should clarify what the requirements are. Your question only mentions the constraint that the input is lowercase-alpha, that the function should be order-invariant and that "ba" and "c" shouldn't collide, which is fairly little information. Commented Jul 12, 2017 at 11:20
  • Why don't you sort the characters in the input string, and then use any standard hashing algorithm? Commented Jul 12, 2017 at 11:23
  • Anything commutative? xor(hash(x), hash(reverse(x))) Commented Jul 12, 2017 at 11:27
  • Possible dupe: stackoverflow.com/questions/30734848/… Commented Jul 12, 2017 at 11:28

3 Answers 3

2

The first thing that comes to mind in the vein of the attempt in the question is to assign every letter a prime number, and multiply them. Then, "ab" is 2*3 = 6; "ba" is 3*2 = 6; "c" is 5.

Sign up to request clarification or add additional context in comments.

1 Comment

One should note that, while this is a good answer in theory (it is collision free if your integers are arbitrarily large), it is of disadvantage in practical applications if you hash values have many (small) divisors. Hashmaps usually use the hash values mod n (for some n that can vary). Having many divisors can lead to many collisions mod n.
1

No, because there are infinitely many possible Strings, but there is only a finite number of possible Hash values.

You cannot have collision free Hash functions on Strings, but you can design your function to have as little collision as possible for the expected input values.

Comments

0

As others have mentioned, you can't ensure that every string with different letters produces a different hash, because there are only 2^32 (or 2^64) different hashes available, and way more different combinations of letters than that.

But if you just want to make a hash function that doesn't care about the order of the characters in the string, then the simplest thing to do is sort the characters in the string (so "canada" would become "aaacdn", for example), and then hash the result.

Another common way is to map each character to a random-looking number, and then just add together the numbers for all the characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.