set M = len(list1) and N = len(list2)
For each of the N entries in list2 you will need to perform M comparisons with the items in list1 . This is the worst running time of O(M x N) . If you take it further, let each entry in list2 have a length of 1, and each entry in list1 will have a length of 300, then you will get an O(300M x N) runtime.
If performance is really a problem, try dynamic programming. Here is the start:
1) sort list2 in increasing order of length:
['scorch', 'scorching', 'dump', 'dumpster', 'dumpsters']
2) sort them in the sublists so that each previous record is a subset of the existing record as follows:
[['scorch', 'scorching'] , ['dump', 'dumpster', 'dumpsters']]
3) Now, if you compare with list1 and 'scorch' , there is none, then you do not need to search for 'scorching' . Similarly, if 'dump' is not there, neither 'dumpster' or 'dumpsters'
Note that the worst execution time of the program remains the same.
source share