> Wouldn't the following avoid it altogether? > > return (len(t) == j-i) and (s.find(t,i) != -1) I think you'd want (s.find(t,i) == i) instead.