Given an array of strings arr[], the task is to find the count of distinct strings present in the array using polynomial rolling hash function.
Examples:
Input: arr[] = { “abcde”, “abcce”, “abcdf”, “abcde”, “abcdf” }
Output: 3
Explanation:
Distinct strings in the array are { “abcde”, “abcce”, “abcdf” }.
Therefore, the required output is 3.Input: arr[] = { “ab”, “abc”, “abcd”, “abcde”, “a” }
Output: 5
Explanation:
Distinct strings in the array are { “abcde”, “abcd”, “abc”, “ab”, “a” }.
Therefore, the required output is 5.
Approach: The problem can be solved using Hashing. The idea is to use rolling hash function to calculate the hash value of all the strings of the array and store it in another array, say Hash[]. Finally, print the count of distinct elements in Hash[] array. Follow the steps below to solve the problem:
- Initialize an array, say Hash[], to store the hash value of all the strings present in the array using rolling hash function.
- Initialize a variable, say cntElem, to store the count of distinct strings present in the array.
- Traverse the array arr[]. For every string encountered, calculate the hash value of that string and store it in the hash[] array.
- Sort the array hash[].
- Traverse the array hash[]. For every array element, check if hash[i] and hash[i – 1] are equal or not. If found to be false, then increment cntElem by 1.
- Finally, print the value of cntElem.
Below is the implementation of the above approach:
CPP
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the hash value // of a string long long compute_hash(string str) { int p = 31; int MOD = 1e9 + 9; long long hash_val = 0; long long mul = 1; // Traverse the string for (char ch : str) { // Update hash_val hash_val = (hash_val + (ch - 'a' + 1) * mul) % MOD; // Update mul mul = (mul * p) % MOD; } // Return hash_val of str return hash_val; } // Function to find the count of distinct // strings present in the given array int distinct_str(vector<string>& arr, int n) { // Store the hash values of // the strings vector<long long> hash(n); // Traverse the array for (int i = 0; i < n; i++) { // Stores hash value of arr[i] hash[i] = compute_hash(arr[i]); } // Sort hash[] array sort(hash.begin(), hash.end()); // Stores count of distinct // strings in the array int cntElem = 1; // Traverse hash[] array for (int i = 1; i < n; i++) { if (hash[i] != hash[i - 1]) { // Update cntElem cntElem++; } } return cntElem; } // Driver Code int main() { vector<string> arr{ "abcde", "abcce", "abcdf", "abcde" }; int N = arr.size(); cout << distinct_str(arr, N) << endl; return 0; } |
3
Time Complexity: O(N * M), where M is the maximum length of the string
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- String hashing using Polynomial rolling hash function
- What are Hash Functions and How to choose a good Hash Function?
- Puzzle | Choose the game of Rolling Dice
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Count clockwise array rotations required to maximize count of array elements present at indices same as their value
- Solving Homogeneous Recurrence Equations Using Polynomial Reduction
- Partial derivative of a polynomial using Doubly Linked List
- Polynomial Division using Linked List
- Print all Unique Strings present in a given Array
- Distinct strings such that they contains given strings as sub-sequences
- Count of anagrams of each string in an array present in another array
- Sorting using trivial hash function
- Count the number of strings in an array whose distinct characters are less than equal to M
- Sort an array of strings based on count of distinct characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Horner's Method for Polynomial Evaluation
- Sgn value of a polynomial
- Finding nth term of any Polynomial Sequence
- Python | Finding Solutions of a Polynomial Equation
- Program to find the indefinite Integration of the given Polynomial
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.

