Z-algorithm matching count

Write codeMedium25 min

Your task

For a string s, consider each starting index i (including 0). Index i counts if the substring of s that begins at i shares at least one leading character with s itself, equivalently, the Z-value z[i] is positive (and z[0] is the whole string by convention). Return how many indices count. For example, in "aabaab" the qualifying indices are 0, 1, 3, 4, so the answer is 4. The string can be up to 10^5 characters; aim for linear time.

Examples

count_prefix_matches(s: str) -> int
Example 1
Inputcount_prefix_matches("aabaab")
Output4