I have created a test case with 90000 elements which produces about 2000 "Hits". Without printing, your original code processes it in less than 1 second. I tried adding the substring recognition to Laurent's approach. I find it much easier to understand, but it is slightly slower than yours. Note: Your original code does not find any substrings longer than 8 (not the ten in your spec).