-2

I can use this hash function for strings but the problem with it is that anagrams will have the same hash value. I want to modify this so that they don't have the same hash values.

def hash(a_string, table_size):
sum = 0
for pos in range(len(a_string)):
  sum = sum + ord(a_string[pos])

return sum % table_size
NefP6
  • 1
  • 1
    This doesn't seem to be a c++ related question, nor is the code. As for string hashing, have you tried looking on the internet? There's a whole number of algorithms that can be used. Maybe here is some inspiration? https://stackoverflow.com/questions/7666509/hash-function-for-string – Jane Doe Jun 01 '22 at 19:37
  • Find a more sophisticated hash function. There are plenty: https://en.wikipedia.org/wiki/Category:Hash_functions . Also, this is not C++, please retag . – Seva Alekseyev Jun 01 '22 at 19:38
  • Just use `std::hash`. – Quimby Jun 01 '22 at 19:38

0 Answers0