My code is as follows def substrings(a, b, n): """Return substrings of length n in both a and b""" similar = [] lista = [] listb = [] crawla = 0 crawlb = 0 while (crawla + n) < len(a): # While there are substrings left to iterate lista.append(a[crawla:crawla + n]) # Starting from 0, append chars from crawla to crawla + n crawla += 1 while (crawlb + n) < len(b): listb.append(b[crawlb:crawlb + n]) crawlb += 1 for sbstr in lista: if sbstr in listb and sbstr not in similar: similar.append(sbstr) # TODO return (similar) # Returns similar sentences

It seems to work as intended for the most part, however the amount of matching substrings I'm returning is inaccurate. I'm not really sure why as when I tried going over it manually in my head it seems accurate.



Use <= len(a) and <= len(b). You always ignore that last substring, that's why "bar" isn't part of "foobar".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .