Given two strings ‘X’ and ‘Y’, print the length of the longest common substring. If two or more substrings have the same value for longest common substring, then print any one of them.
Input : X = "GeeksforGeeks", Y = "GeeksQuiz" Output : Geeks Input : X = "zxabcdezy", Y = "yzabcdezx" Output : abcdez
We have discussed a solution to find length of longest common string. In this post, we have discussed printing common string is discussed.
Naive Approach: Let strings X and Y be the lengths m and n respectively. Generate all possible substrings of X which requires a time complexity of O(m2) and search each substring in the string Y which can be achieved in O(n) time complexity using KMP algorithm. Overall time complexity will be O(n * m2).
Efficient Approach: It is based on the dynamic programming implementation explained in this post. The longest suffix matrix LCSuff is build up and the index of the cell having the maximum value is tracked. Let that index be represented by (row, col) pair. Now the final longest common substring is build with the help of that index by diagonally traversing up the LCSuff matrix until LCSuff[row][col] != 0 and during the iteration obtaining the characters either from X[row-1] or Y[col-1] and adding them from right to left in the resultant common string.
Time Complexity: O(m*n).
Auxiliary Space: O(m*n).
Space Optimized Approach:
The auxiliary space used by the solution above is O(m*n), where m and n are lengths of string X and Y. The space used by the above solution can be reduced to O(2*n). A variable end is used to store ending point of the longest common substring in string X and variable maxlen is used to store the length of the longest common substring.
Suppose we are at DP state when the length of X is i and length of Y is j, the result of which is stored in len[i][j].
Now if X[i-1] == Y[j-1], then len[i][j] = 1 + len[i-1][j-1], that is result of current row in matrix len depends on values from previous row. Hence the required length of longest common substring can be obtained by maintaining values of two consecutive rows only, thereby reducing space requirements to O(2*n).
To print the longest common substring, we use variable end. When len[i][j] is calculated, it is compared with maxlen. If maxlen is less than len[i][j], then end is updated to i-1 to show that longest common substring ends at index i-1 in X and maxlen is updated to len[i][j]. The longest common substring then is from index end – maxlen + 1 to index end in X.
A variable currRow is used to represent that either row 0 or row 1 of len[n] matrix is currently used to find the length. Initially, row 0 is used as the current row for the case when the length of string X is zero. At the end of each iteration, the current row is made the previous row and the previous row is made the new current row.
Given below is the implementation of the above approach:
Time Complexity: O(m*n)
Auxiliary Space: O(n)
This approach has been suggested by nik1996.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all the duplicates in the input string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Print list items containing all characters of a given word
- Longest Common Subsequence | DP-4
- Length of the longest substring without repeating characters
- Longest Non-palindromic substring
- Print all permutations with repetition of characters
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Longest Palindromic Subsequence | DP-12
- Print all permutations in sorted (lexicographic) order
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Longest Common Substring | DP-29
- Print all ways to break a string in bracket form
- Printing Longest Common Subsequence
- Print all possible words from phone digits