Skip to main content

String Hashing

Code

We need a hash function to convert a string into an integer, called the hash of that string. The following condition has to hold: For two strings and ,

but reverse is not guaranteed. We only hope that

Polynomial Rolling Hash Function

where and and are some chosen, positive numbers.

caution

For example while calculating the hash of string made of only lowercase letters, we convert each character of to an integer. A possible conversion is . Converting is not a good idea, because then the hashes of the strings all evaluate to 0.

// Precomputing the powers of p might give a performance boost.

Technique: Fast Polynomial Rolling Hash of Substrings