text. Take every non-empty suffix of it, from the whole string down to the last character, and find the length of the longest common prefix of that suffix and text itself.
Return the sum of these lengths over all suffixes.
Comparing each suffix with the start directly takes O(n²); aim for something faster.text="abcab" → 7
The tails: abcab matches the start for 5 characters, bcab for 0, cab for 0, ab for 2, b for 0. That totals 7.
text="qqq" → 6
The tails qqq, qq and q each match the start in full: 3 + 2 + 1 = 6.
text="xyz" → 3
text consists of lowercase English letterstext="abcab"
7
Run checks the first 3 tests in your browser. Submit checks all 126 tests on the server, hidden ones included.