Given a string, count the number of distinct substrings using Rabin Karp Algorithm.
Input : str = “aba” Output : 5 Explanation : Total number of distinct substring are 5 - "a", "ab", "aba", "b" ,"ba" Input : str = “abcd” Output : 10 Explanation : Total number of distinct substring are 10 - "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d"
Prerequisite: Rabin-Karp Algorithm for Pattern Searching
Calculate the current hash value of the current character and store
in a dictionary/map to avoid repetition.
To compute the hash (rolling hash) as done in Rabin-Karp algorithm follow:
The hash function suggested by Rabin and Karp calculates an integer value. The integer value for a string is numeric value of a string. For example, if all possible characters are from 1 to 10, the numeric value of “122” will be 122. The number of possible characters is higher than 10 (256 in general) and pattern length can be large. So the numeric values cannot be practically stored as an integer. Therefore, the numeric value is calculated using modular arithmetic to make sure that the hash values can be stored in an integer variable (can fit in memory words). To do rehashing, we need to take off the most significant digit and add the new least significant digit for in hash value. Rehashing is done using the following formula.
hash( txt[s+1 .. s+m] ) = ( d ( hash( txt[s .. s+m-1]) – txt[s]*h ) + txt[s + m] ) mod q
hash( txt[s .. s+m-1] ) : Hash value at shift s.
hash( txt[s+1 .. s+m] ) : Hash value at next shift (or shift s+1)
d: Number of characters in the alphabet
q: A prime number
The idea is similar as we evaluate a mathematical expression. For example, we have a string of “1234” let we compute the value of the substring “12” is 12 and we want to compute the value of the substring “123” this can be calculated as ((12)*10+3)=123, similar logic is applied here.
10 a ab abc abcd b bc bcd c cd d
Time Complexity: O(N2), N is the length of string
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.
- Rabin-Karp algorithm for Pattern Searching in Matrix
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Count of Distinct Substrings occurring consecutively in a given String
- Find distinct characters in distinct substrings of a string
- Count of substrings having all distinct characters
- Count distinct substrings that contain some characters at most k times
- Count number of distinct substrings of a given length
- Count of substrings of length K with exactly K distinct characters
- Count number of substrings with exactly k distinct characters
- Minimum changes to a string to make all substrings distinct
- Count of bitonic substrings from the given string
- Count of substrings of a binary string containing K ones
- Count of substrings of a given Binary string with all characters same
- Count of Reverse Bitonic Substrings in a given String
- Count number of substrings of a string consisting of same characters
- Queries to find the count of vowels in the substrings of the given string
- Count the number of vowels occurring in all the substrings of given string
- Permutation of given string that maximizes count of Palindromic substrings
- Given a binary string, count number of substrings that start and end with 1.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.