Code Room
CodingMediumcod-g146
Subject Z algorithmLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Implement
count_prefix_matches(s: str) → int
Examples
in["aabaab"]out4
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.