
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SequenceMatcher in Python for Longest Common Substring.
The SequenceMatcher class is the part of the Python difflib module. It is used to compare sequence (such as lists or strings) and finds the similarities between them.
The task is to find the Longest Common Substring, i.e, the longest sequence of the characters that appears contiguously in both strings. which is different from the Longest Common Subsequence, where the characters may appear in the same order but not repeatedly.
Using find_longest_match() Method
The find_longest_match() method is used to find the longest matching sequence of elements between two sequences. It is the part of the Python difflib module.
Example 1
Let's look at the following example, where we are going to perform the basic match to find the longest common substring between "abcde" and "abghf".
from difflib import SequenceMatcher x = "abcde" y = "abghf" a = SequenceMatcher(None, x, y) result = a.find_longest_match(0, len(x), 0, len(y)) print("Result:", x[result.a : result.a + result.size])
The output of the above program is as follows -
Result: ab
Example 2
Consider the following example, where we are going to find the longest common substring between "xyz" and "efg", as there is no common substring present the output will be the empty string.
from difflib import SequenceMatcher x = "xyz" y = "efg" a = SequenceMatcher(None, x, y) result = a.find_longest_match(0, len(x), 0, len(y)) match = x[result.a : result.a + result.size] print("Result: '{}'".format(match))
The output of the above program is as follows -
Result: ''
Example 3
Following is the example, where we are going to use the case-sensitive and finding te longest common substring between 'Welcome' and 'weLCome'.
from difflib import SequenceMatcher x = "Welcome" y = "weLCome" a = SequenceMatcher(None, x, y) result = a.find_longest_match(0, len(x), 0, len(y)) print("Result:", x[result.a : result.a + result.size])
The output of the above program is as follows -
Result: ome