SequenceMatcher in Python for Longest Common Substring Last Updated : 24 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given two strings ‘X’ and ‘Y’, print the longest common sub-string. Examples: 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). Implementation: Python3 # Function to find Longest Common Sub-string from difflib import SequenceMatcher def longestSubstring(str1,str2): # initialize SequenceMatcher object with # input string seqMatch = SequenceMatcher(None,str1,str2) # find match of longest sub-string # output will be like Match(a=0, b=0, size=5) match = seqMatch.find_longest_match(0, len(str1), 0, len(str2)) # print longest substring if (match.size!=0): print (str1[(match.a: match.a + match.size)]) else: print ('No longest common sub-string found') # Driver program if __name__ == "__main__": str1 = 'GeeksforGeeks' str2 = 'GeeksQuiz' longestSubstring(str1,str2) OutputGeeks Comment More infoAdvertise with us Next Article SequenceMatcher in Python for Longest Common Substring S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-string Python-DSA +1 More Practice Tags : pythonStrings Similar Reads Longest Common Subsequence | DP using Memoization Given two strings s1 and s2, the task is to find the length of the longest common subsequence present in both of them. Examples: Input: s1 = âABCDGHâ, s2 = âAEDFHRâ Output: 3 LCS for input Sequences âAGGTABâ and âGXTXAYBâ is âGTABâ of length 4. Input: s1 = âstriverâ, s2 = ârajâ Output: 1 The naive s 13 min read Longest Common Substring (Space optimized DP solution) Given two strings âs1â and âs2â, find the length of the longest common substring. Example: Input: s1 = âGeeksforGeeksâ, s2 = âGeeksQuizâ Output : 5 Explanation:The longest common substring is âGeeksâ and is of length 5.Input: s1 = âabcdxyzâ, s2 = âxyzabcdâ Output : 4Explanation:The longest common su 7 min read Print the longest common substring Given two strings âXâ and âYâ, print the length of the longest common substring. If two or more substrings have the same value for the longest common substring, then print any one of them. Examples: Input : X = "GeeksforGeeks", Y = "GeeksQuiz" Output : Geeks Input : X = "zxabcdezy", Y = "yzabcdezx" 15+ min read String Subsequence and Substring in Python Subsequence and Substring both are parts of the given String with some differences between them. Both of them are made using the characters in the given String only. The difference between them is that the Substring is the contiguous part of the string and the Subsequence is the non-contiguous part 5 min read Longest Common Substring Given two strings 's1' and 's2', find the length of the longest common substring. Example: Input: s1 = "GeeksforGeeks", s2 = "GeeksQuiz" Output : 5 Explanation:The longest common substring is "Geeks" and is of length 5.Input: s1 = "abcdxyz", s2 = "xyzabcd" Output : 4Explanation:The longest common su 15+ min read Like