Apex › Storage & databases

The Z-algorithm — linear-time string matching

One elegant array finds every occurrence of a pattern in a text in linear time. Z[i] measures how much of the string's prefix reappears at position i; run it over "pattern § text" and every spot where Z ≥ |pattern| is a match. Type a pattern and text and watch the Z-values light up the matches — overlaps included.

More in Storage & databases