How to find all intersections (also called the longest common substrings) of two strings and their positions in both strings?
For example, if S1="never"
and S2="forever"
then resulted intersection must be ["ever"]
and its positions are [(1,3)]
. If S1="address"
and S2="oddness"
then resulted intersections are ["dd","ess"]
and their positions are [(1,1),(4,4)]
.
Shortest solution without including any library is preferable. But any correct solution is also welcomed.
Batteries included!
The difflib module might have some help for you - here is a quick and dirty side-by-side diff:
I'm assuming you only want substrings to match if they have the same absolute position within their respective strings. For example, "abcd", and "bcde" won't have any matches, even though both contain "bcd".
positions =
[1, 2, 4, 5, 6]
substrings =
['dd', 'ess']
If you only want substrings, you can squish it into one line:
Here's what I could come up with:
Checking some values:
This can be done in O(n+m) where
n
andm
are lengths of input strings.The pseudocode is:
See the Longest_common_substring_problem wikipedia article for more details.
Well, you're saying that you can't include any library. However, Python's standard difflib contains a function which does exactly what you expect. Considering that it is a Python interview question, familiarity with difflib might be what the interviewer expected.
You can always ignore the last Match tuple, since it's dummy (according to documentation).