Z-function

Find where the beginning of a word repeats and how many letters match there. In барабан, starting at the second б gives ба again, a match of two letters.

In “барабан”, both occurrences of “б” start with “ба”. The next letters differ, so the second “б” gets 2. Reuse a known matching interval for the next cells.