Given two strings ‘X’ and ‘Y’, print the longest common sub-string.
Input : X = "GeeksforGeeks", Y = "GeeksQuiz" Output : Geeks Input : X = "zxabcdezy", Y = "yzabcdezx" Output : abcdez
We have existing solution for this problem please refer Print the longest common substring link. We will solve problem in python using SequenceMatcher.find_longest_match() method.
How SequenceMatcher.find_longest_match(aLow,aHigh,bLow,bHigh) method works ?
First we initialize SequenceMatcher object with two input string str1 and str2, find_longest_match(aLow,aHigh,bLow,bHigh) takes 4 parameters aLow, bLow are start index of first and second string respectively and aHigh, bHigh are length of first and second string respectively. find_longest_match() returns named tuple (i, j, k) such that a[i:i+k] is equal to b[j:j+k], if no blocks match, this returns (aLow, bLow, 0).
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.
- Longest Common Substring | DP-29
- Print the longest common substring
- Longest Common Substring in an Array of Strings
- Longest Common Substring (Space optimized DP solution)
- Longest common substring in binary representation of two numbers
- Python | Find longest consecutive letter and digit substring
- Check if two strings have a common substring
- Longest Common Subsequence | DP-4
- Longest substring of vowels
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Longest Non-palindromic substring
- Longest Common Extension / LCE | Set 2 ( Reduction to RMQ)
- Longest Common Prefix using Trie
- LCS (Longest Common Subsequence) of three strings
- Longest Common Anagram Subsequence
- Longest Common Prefix using Sorting
- Longest Common Subsequence | DP using Memoization
- Longest Common Prefix Matching | Set-6
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.