Given two strings ‘X’ and ‘Y’, find the length of the longest common substring.
Input : X = “GeeksforGeeks”, y = “GeeksQuiz”
Output : 5
The longest common substring is “Geeks” and is of length 5.
Input : X = “abcdxyz”, y = “xyzabcd”
Output : 4
The longest common substring is “abcd” and is of length 4.
Input : X = “zxabcdezy”, y = “yzabcdezx”
Output : 6
The longest common substring is “abcdez” and is of length 6.
Let m and n be the lengths of first and second strings respectively.
A simple solution is to one by one consider all substrings of first string and for every substring check if it is a substring in second string. Keep track of the maximum length substring. There will be O(m^2) substrings and we can find whether a string is subsring on another string in O(n) time (See this). So overall time complexity of this method would be O(n * m2)
Dynamic Programming can be used to find the longest common substring in O(m*n) time. The idea is to find length of the longest common suffix for all substrings of both strings and store these lengths in a table.
The longest common suffix has following optimal substructure property.
If last characters match, then we reduce both lengths by 1
LCSuff(X, Y, m, n) = LCSuff(X, Y, m-1, n-1) + 1 if X[m-1] = Y[n-1]
If last characters do not match, then result is 0, i.e.,
LCSuff(X, Y, m, n) = 0 if (X[m-1] != Y[n-1])
Now we consider suffixes of different substrings ending at different indexes.
The maximum length Longest Common Suffix is the longest common substring.
LCSubStr(X, Y, m, n) = Max(LCSuff(X, Y, i, j)) where 1 <= i <= m and 1 <= j <= n
Following is the iterative implementation of the above solution.
Length of Longest Common Substring is 10
Time Complexity: O(m*n)
Auxiliary Space: O(m*n)
Another approach: (Using recursion)
Here is the recursive solution of above approach.
The longest substring can also be solved in O(n+m) time using Suffix Tree. We will be covering Suffix Tree based solution in a separate post.
Exercise: The above solution prints only length of the longest common substring. Extend the solution to print the substring also.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Longest common substring in binary representation of two numbers
- SequenceMatcher in Python for Longest Common Substring
- Longest Common Substring (Space optimized DP solution)
- Print the longest common substring
- Longest Common Substring in an Array of Strings
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Check if two strings have a common substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Longest Common Subsequence | DP-4
- Printing Longest Common Subsequence
- Longest Common Prefix using Word by Word Matching
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Common Prefix using Character by Character Matching
- Longest Common Prefix using Divide and Conquer Algorithm
- Longest Common Prefix using Binary Search
- Longest Common Prefix using Trie
- Printing Longest Common Subsequence | Set 2 (Printing All)