Code RoomZ-algorithm sum
MediumPrep Room Coding #1188

Z-algorithm sum

CodingAlgorithms & data structuresMid–Senior~25 min

Given a string s, return the SUM over all indices i of the length of the longest common prefix between s and the suffix of s starting at i (for i=0 this is len(s) itself). 1 <= len(s) <= 100000, lowercase letters. Example: 'ababa' -> z = [5,0,3,0,1], sum = 9.

Implement
sum_z_values(s: str) → int
Examples
in["ababa"]out9
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.

0:00 of about 25 min
InputExpectedGot
["ababa"]9not run yetsample