python find if strings have common substring

Solutions on MaxInterview for python find if strings have common substring by the best coders in the world

showing results for - "python find if strings have common substring"
Kurt
04 Jun 2017
1# credit to the Stack Overflow user in the source link
2
3from difflib import SequenceMatcher
4
5string1 = "apple pie available"
6string2 = "come have some apple pies"
7
8match = SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2))
9
10print(match)  # -> Match(a=0, b=15, size=9)
11print(string1[match.a: match.a + match.size])  # -> apple pie
12print(string2[match.b: match.b + match.size])  # -> apple pie
similar questions