Join the pattern, a separator and the text: "aab$aabxaabaab". z[i] is the length of the longest substring starting at i that equals a prefix of the whole string. The separator keeps any match at most 3 long. Wherever z[i] = 3 the text contains the pattern. The box [l, r] is the rightmost match found so far. Inside it, z values can be copied from their mirror image near the start instead of compared from scratch.
Z algorithm over 14 characters.
i = 1. Outside the box, so compare from scratch. Comparing extends it by 1. z[1] = 1. That reaches further right than the box, so the box becomes [1, 1].
z at 1 is 1.
i = 2. Outside the box, so compare from scratch. The next comparison fails at once. z[2] = 0.
z at 2 is 0.
i = 3. Outside the box, so compare from scratch. The next comparison fails at once. z[3] = 0. That reaches further right than the box, so the box becomes [3, 2].
z at 3 is 0.
i = 4. Outside the box, so compare from scratch. Comparing extends it by 3. z[4] = 3: the full pattern, a match in the text at index 0. That reaches further right than the box, so the box becomes [4, 6].
z at 4 is 3.
i = 5. Inside the box [4, 6], mirror position 1 has z = 1 and the box has 2 left, so start from 1. The next comparison fails at once. z[5] = 1.
z at 5 is 1.
i = 6. Inside the box [4, 6], mirror position 2 has z = 0 and the box has 1 left, so start from 0. The next comparison fails at once. z[6] = 0.
z at 6 is 0.
i = 7. Outside the box, so compare from scratch. The next comparison fails at once. z[7] = 0.
z at 7 is 0.
i = 8. Outside the box, so compare from scratch. Comparing extends it by 3. z[8] = 3: the full pattern, a match in the text at index 4. That reaches further right than the box, so the box becomes [8, 10].
z at 8 is 3.
i = 9. Inside the box [8, 10], mirror position 1 has z = 1 and the box has 2 left, so start from 1. The next comparison fails at once. z[9] = 1.
z at 9 is 1.
i = 10. Inside the box [8, 10], mirror position 2 has z = 0 and the box has 1 left, so start from 0. The next comparison fails at once. z[10] = 0.
z at 10 is 0.
i = 11. Outside the box, so compare from scratch. Comparing extends it by 3. z[11] = 3: the full pattern, a match in the text at index 7. That reaches further right than the box, so the box becomes [11, 13].
z at 11 is 3.
i = 12. Inside the box [11, 13], mirror position 1 has z = 1 and the box has 2 left, so start from 1. The next comparison fails at once. z[12] = 1.
z at 12 is 1.
i = 13. Inside the box [11, 13], mirror position 2 has z = 0 and the box has 1 left, so start from 0. The next comparison fails at once. z[13] = 0.
z at 13 is 0.
Found at 0, 4, 7. 22 character comparisons for 13 z values. Every successful comparison pushes r further right and r never moves back, so the whole array costs O(n + m), like KMP but with a table that is easier to read: z[i] says how much of the prefix reappears at i.
Matches at 0, 4, 7.