Ctrl K

Suffixes Echoing the Start

Hard
You are given a string 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.
Example 1
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.

Example 2
text="qqq" → 6

The tails qqq, qq and q each match the start in full: 3 + 2 + 1 = 6.

Example 3
text="xyz" → 3
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeated Fragment HardWord Near an Anchor HardPhrase From Word Beginnings, Long Edition HardLongest Piece Seen Twice MediumAnchor Near a Companion Medium
loading Python…
Input
text="abcab"
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 126 tests on the server, hidden ones included.