2-Dimensional String Search

In my previous two posts, I implemented KMP search with continuation and the Aho-Corasick dictionary. Now we can put it all together and perform a 2D search, finding a rectangular needle in a larger rectangular haystack. This implementation runs in O(haystack_size + needle_size).

The basic steps:

1. Create Aho-Corasick dictionary from needle rows.

2. Haystack states: for each row of the haystack, for each character run it through our dictionary and record each state. (That’s one state per character.)

3. Needle fingerprint: for each row of the needle, run it through the dictionary and record the very final state. (That’s one state per row.)

4. Finally, search the haystack states down the columns for the needle fingerprint using KMP.

The implementation is in git and below:

Leave a Reply