String hashing using Polynomial rolling hash function

What is String-Hashing? 

String hashing is the way to convert a string into an integer known as a hash of that string.
An ideal hashing is the one in which there are minimum chances of collision (i.e 2 different strings having the same hash).

Polynomial rolling hash function

In this hashing technique, the hash of a string is calculated as:

hash = (s[0]*P^{0} + s[1]*P^{1} + ....s[m]*P^{m}) mod M

Where P and M are some positive numbers.  And s[0], s[1], s[2] … s[n-1] are the values assigned to each character in English alphabet (a->1, b->2, … z->26). 

Appropriate values for P and M 



  • P: The value of P can be any prime number roughly equal to the number of different characters used. 
    For example: if the input string contains only lowercase letters of the English alphabet, then P = 31 is the appropriate value of P. 
    If the input string contains both uppercase and lowercase letters, then P = 53 is an appropriate option.
  • M: the probability of two random strings colliding is inversely proportional to m, Hence m should be a large prime number. 
    M = 10 ^9 + 9 is a good choice.

Below is the implementation of the String hashing using the Polynomial hashing function:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the
// Polynomial Rolling Hash Function
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// the hash of a string
long long polynomialRollingHash(
    string const& str)
{
    // P and M
    int p = 31;
    int m = 1e9 + 9;
    long long power_of_p = 1;
    long long hash_val = 0;
 
    // Loop to calculate the hash value
    // by iterating over the elements of string
    for (int i = 0; i < str.length(); i++) {
        hash_val
            = (hash_val
               + (str[i] - 'a' + 1) * power_of_p)
              % m;
        power_of_p
            = (power_of_p * p) % m;
    }
    return hash_val;
}
 
/// Driver Code
int main()
{
    // Given strings
    string str1 = "geeksforgeeks";
    string str2 = "geeks";
 
    cout << "Hash of '" << str1 << "' = "
         << polynomialRollingHash(str1);
    cout << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the
// Polynomial Rolling Hash Function
class GFG{
 
// Function to calculate
// the hash of a String
static long polynomialRollingHash(String str)
{
     
    // P and M
    int p = 31;
    int m = (int)(1e9 + 9);
    long power_of_p = 1;
    long hash_val = 0;
 
    // Loop to calculate the hash value
    // by iterating over the elements of String
    for(int i = 0; i < str.length(); i++)
    {
        hash_val = (hash_val + (str.charAt(i) -
                    'a' + 1) * power_of_p) % m;
        power_of_p = (power_of_p * p) % m;
    }
    return hash_val;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Strings
    String str1 = "geeksforgeeks";
    String str2 = "geeks";
 
    System.out.print("Hash of '" + str1 + "' = " +
                     polynomialRollingHash(str1));
}
}
 
// This code is contributed by Amit Katiyar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the
# Polynomial Rolling Hash Function
 
# Function to calculate
# the hash of a string
def polynomialRollingHash(str):
     
    # P and M
    p = 31
    m = 1e9 + 9
    power_of_p = 1
    hash_val = 0
 
    # Loop to calculate the hash value
    # by iterating over the elements of string
    for i in range(len(str)):
        hash_val = ((hash_val + (ord(str[i]) -
                                 ord('a') + 1) *
                              power_of_p) % m)
 
        power_of_p = (power_of_p * p) % m
 
    return int(hash_val)
 
# Driver Code
if __name__ == '__main__':
 
    # Given string
    str1 = "geeksforgeeks"
 
    print("Hash of '{}' = {}".format(
          str1, polynomialRollingHash(str1)))
 
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the
// Polynomial Rolling Hash Function
using System;
class GFG{
 
// Function to calculate
// the hash of a String
static long polynomialRollingHash(String str)
{   
    // P and M
    int p = 31;
    int m = (int)(1e9 + 9);
    long power_of_p = 1;
    long hash_val = 0;
 
    // Loop to calculate the hash value
    // by iterating over the elements of String
    for(int i = 0; i < str.Length; i++)
    {
        hash_val = (hash_val + (str[i] -
                    'a' + 1) * power_of_p) % m;
        power_of_p = (power_of_p * p) % m;
    }
    return hash_val;
}
 
// Driver Code
public static void Main(String[] args)
{   
    // Given Strings
    String str1 = "geeksforgeeks";
    String str2 = "geeks";
 
    Console.Write("Hash of '" + str1 + "' = " +
                   polynomialRollingHash(str1));
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

Hash of 'geeksforgeeks' = 111226362



competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.