In this exercise, the hashing function will always return the same value for anagrams, like “name” and “mean”. What if we want to account for the specific ordering of the letters in the strings?
If we wanted to account for the specific ordering of the letters in the strings, so that anagrams do not always return the same hash value, one method is to consider the specific index of each letter when computing the hash value.
For instance, when adding the point values, we can multiply each point with their index + 1, before getting the sum.
# Both words give us the same code points # name -> ['110', '97', '109', '101'] # mean -> ['109', '101', '97', '110'] # Which give us the same hash value of 417 # By adding weights to each letter position, # we would get these values instead. # name: # 110*1 + 97*2 + 109*3 + 101*4 = 1035 # mean: # 109*1 + 101*2 + 97*3 + 110*4 = 1042
This is just one of the methods you might use to take into account similar keys such as anagrams. This can also apply to numerical values, where instead of indexes, you might take into account the digit positions.