Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
String Hashing
What is Hash Function?
Problem Statement
Output 1
Input 2
Output 2
Time Complexity
Space Complexity
Key Takeaways
Last Updated: Mar 27, 2024

String Hashing

Author Ujjawal Gupta
1 upvote
Create a resume that lands you SDE interviews at MAANG
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM


In this blog, we will discuss the topic of string hashing. String hashing is a very interesting as well as an important topic. String hashing based-coding problems are widely asked in coding contests and various coding interviews. 

String Hashing

String hashing is the technique or process of mapping string and integer. In other words, String hashing is the way to convert the string into an integer, and that integer is known as the hash of a string.

String hashing is used to compare two strings whether they are equal or not. They both are equal if the hash value of both the string is equal. The hash value of the string is calculated by using a hash function. Let’s discuss the hash function to convert a string into an integer.

What is Hash Function?

String hash function is the function that converts a string into an integer. The hash of a string can be calculated as:

Hash(S) =(S[0] * P^0 + S[1] * P^1 +S[2] * P^2 + …….+ S[N-1] * P^(N-1)) % M


 ‘S’ is the given string.

 ‘P’ is the prime number that must be greater than or equal to the number of distinct characters in the string ‘S’.

 ‘M’ should be large to avoid the collision. 

The above function is known as the polynomial rolling hash function.


It may be possible that two different strings have the same hash values. This may occur because we take modulo ‘M’ in the final hash value. In that case, two different strings may have the same hash values, called a collision. We need to design our hash function so that the collision probability is very low. One way to reduce the chances of collision is that not take the mod of the final hash value with ‘M’. But this is not efficient as the hash value may be very large, and our storage memory is limited.

Another way to reduce the collision probability is to take the value of ‘M’ as large as possible. 

The probability of collision is 1 / M.


The hash of string S = “cat” can be calculated as:

hash(S) = (‘c’ - ‘a’) + (‘a’ - ‘a’) * 31 + (‘c’ - ‘a’) * 31^2.

Here, P = 31 as all the characters are in lowercase, so the count of all distinct lowercase characters is 26, and 31 is the prime number and greater than 26. 

Also see, Euclid GCD Algorithm

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Problem Statement

You are given ‘N’ string. Write a program to check whether all the strings are distinct or not using string hashing.


  • Define a function hashing():
    • Function details:
      • Takes string ‘S’ as a parameter.
      • This function returns the hash value of ‘S’.
    • Working: 
      • Create variable ANS = 0.
      • Create variable P = 1.
      • Create variable M = 10e9 +7.
      • Iterate loop for each character of string:
        • ANS += (S[i] - 'a') * P;
        • P *= 31;
      • Return ‘ANS’.
  • Call the hashing function for all strings and store their hash values in the array.
  • Sort the array.
  • Check whether the adjacent hash values are equal or not.


// Program to check whether all the strings are distinct or not using string hashing.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

// Function to find the hash of string.
int hashing(string s)
    // To store the hash value.
    int ans = 0;

    // To store 'P'.
    int p = 1;

    // For taking modulo.
    int m = 1000000007;
    for (int i = 0; i < s.size(); i++)
        ans += (s[i] - 'a') * p;
        ans = ans % m;
        p *= 31;
    return ans;

int main()

    // Taking number of strings as input.
    int n;
    cin >> n;

    // Taking strings as an input.
    vector<pair<int, string>> vec(n);
    for (int i = 0; i < n; i++)
        string temp;
        cin >> temp;

        // Function call.
        vec[i].first = hashing(temp);
        vec[i].second = temp;

    // Sort the vector.
    sort(vec.begin(), vec.end());
    int i;
    for (i = 0; i < n - 1; i++)
        if (vec[i] == vec[i + 1])
    if (i == n - 1)
        cout << "All strings are distinct";
        cout << "All strings are not distinct";

Input 1


Output 1

All strings are not distinct

Input 2


Output 2

All strings are distinct

Time Complexity

O(N * K), where ‘N’ is the number of strings and ‘K’ is the maximum length of string among them.
Hashing function traverses each character of the string, which takes O(K) time. And total number of strings is N. Hence total time complexity for this program is O(N * K).

Space Complexity

As we didn’t use extra space except for a few variables.
Check out this problem - Longest String Chain

Also Read - hash function in data structure

Key Takeaways

In this blog, we learned the concept of string hashing, hash function, and collision. We have started with the discussion of string hashing. After that, we have seen how hash value is calculated, and then we have seen the difficulties we face, i.e., collision and how collision can be reduced.

Therefore learning never stops, and there is a lot more to learn.

Recommended problems -


So head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more. Till then, Happy Coding!

Previous article
Ninja and his Strings
Next article
Maximize the Confusion of an Exam
Live masterclass