VOOZH about

URL: https://www.geeksforgeeks.org/python/sequencematcher-in-python-for-longest-common-substring/

⇱ SequenceMatcher in Python for Longest Common Substring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SequenceMatcher in Python for Longest Common Substring

Last Updated : 24 Mar, 2023

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:


Output
Geeks
Comment
Article Tags: