you could add some for of indexing to save time by not doing checks that will not match for sure. For example you can create 26 lists of indices for "A"
, "B"
, "C"
and so on where the list for character x
contains all indices of strings that contain at least one x
.
When asked to search for a pattern you can check all the letters and pick the index that has the smallest number of indicies and only scan that.
This scheme can be made more sophisticated, for example storing an index list for each pair or triplet of characters. Depending of the number of strings that you need to search the speedup can be huge.
Of course the main assumption is that the list of strings is fixed, has many many elements and that you need to make many searches.