txt1='the heavy lorry crashed into the building at midnight'
txt2='what a heavy lorry it is that crashed into the building'
s1, s2 = txt1.split(), txt2.split()
l1, l2 = len(s1), len(s2)
set1 = {" ".join(s1[j:j+i]) for i in range(1,l1+1) for j in range(l1+1-i)}
set2 = {" ".join(s2[j:j+i]) for i in range(1,l2+1) for j in range(l2+1-i)}
r = sorted(set1.intersection(set2), key = lambda x: len(x.split()))
rs = [j for k,j in enumerate(r) if all(j not in r[i] for i in range(k+1,len(r)))]
print rs
# ['heavy lorry', 'crashed into the building']
source
share