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:
![]()
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++
// C++ implementation of the// Polynomial Rolling Hash Function#include <bits/stdc++.h>using namespace std;// Function to calculate// the hash of a stringlong 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 Codeint main(){ // Given strings string str1 = "geeksforgeeks"; string str2 = "geeks"; cout << "Hash of '" << str1 << "' = " << polynomialRollingHash(str1); cout << endl;} |
Java
// Java implementation of the// Polynomial Rolling Hash Functionclass GFG{// Function to calculate// the hash of a Stringstatic 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 Codepublic 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 |
Python3
# Python3 implementation of the# Polynomial Rolling Hash Function# Function to calculate# the hash of a stringdef 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 Codeif __name__ == '__main__': # Given string str1 = "geeksforgeeks" print("Hash of '{}' = {}".format( str1, polynomialRollingHash(str1)))# This code is contributed by Shivam Singh |
C#
// C# implementation of the// Polynomial Rolling Hash Functionusing System;class GFG{// Function to calculate// the hash of a Stringstatic 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 Codepublic 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 |
Hash of 'geeksforgeeks' = 111226362
Recommended Posts:
- What are Hash Functions and How to choose a good Hash Function?
- Rearrange characters in a string such that no two adjacent are same using hashing
- Complete the sequence generated by a polynomial
- Sgn value of a polynomial
- Program for Derivative of a Polynomial
- Minimize the sum of roots of a given polynomial
- Integration in a Polynomial for a given value
- Content of a Polynomial
- Address Calculation Sort using Hashing
- Area of the largest square that can be formed from the given length sticks using Hashing
- Implement Phone Directory using Hashing
- Hash Function for String data in C#
- Sorting using trivial hash function
- Convert an array to reduced form | Set 1 (Simple and Hashing)
- Cuckoo Hashing - Worst case O(1) Lookup!
- Top 20 Hashing Technique based Interview Questions
- Hashing | Set 1 (Introduction)
- Hashing | Set 2 (Separate Chaining)
- Union and Intersection of two linked lists | Set-3 (Hashing)
- Index Mapping (or Trivial Hashing) with negatives allowed
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.


