Is everybody doing rolling hashes wrong?

They usually use 26 as the "multiply factor" because there are 26 letters in the alphabet, with hash("a") == 0 This is bad because:

"ab" = 0 * 26^1 + 1
"aab" = 0 * 26^2 + 0 * 26^1 + 1 = 1
"aaab" = ... = 1
...
etc.

Isn't it better to let a=1, b=2, ..., z = 26 and have your "multiplication factor" 27? You will run into less tricky hash collisions.

Comments (1)