Find the Longest Common Substring using Binary search and Rolling Hash
Given two strings X and Y, the task is to find the length of the longest common substring.
Input: X = “GeeksforGeeks”, y = “GeeksQuiz”
Explanation: The longest common substring is “Geeks” and is of length 5.
Input: X = “abcdxyz”, y = “xyzabcd”
Explanation: The longest common substring is “abcd” and is of length 4.
Input: X = “zxabcdezy”, y = “yzabcdezx”
Explanation: The longest common substring is “abcdez” and is of length 6.
Longest Common Substring using Dynamic Programming:
This problem can be solved using dynamic programming in O(len(X) * len(Y)), see this. In this article we are going to discuss about an efficient approach.
Longest Common Substring using Binary Search and Rolling Hash
If there is a common substring of length K in both the strings, then there will be common substrings of length 0, 1, …, K – 1. Hence, binary search on answer can be applied.\
Follow the below steps to implement the idea:
- Smallest possible answer(low) = 0 and largest possible answer(high) = min(len(X), len(Y)), Range of binary search will be [0, min(len(X), len(Y))].
- For every mid, check if there exists a common substring of length mid, if exists then update low, else update high.
- To check the existence of a common substring of length K, Polynomial rolling hash function can be used.
- Iterate over all the windows of size K in string X and string Y and get the hash.
- If there is a common hash return True, else return False.
Below is the implementation of this approach.
Time Complexity: O(n * log(m1)) + O(m * log((m1)) + O((n + m) * log(min(n, m)))
- Generating hash object takes O(n*log(m1)), where n is the length of string and m1 = pow(10, 9) + 7.
- Binary search takes O(log(min(n, m))), where n, m are the lengths of both strings.
- Hash of a window takes O(1) time.
- Exist function takes O(n + m) time.
Auxiliary Space: O(n + m)
Please Login to comment...