Suffix Array Builder - Problem

A suffix array is a sorted array of all suffixes of a given string. For example, for string "banana", the suffixes are ["banana", "anana", "nana", "ana", "na", "a"], and the suffix array contains the indices of these suffixes in lexicographically sorted order.

Your task is to:

  1. Build a suffix array for the given string in O(n log n) time complexity
  2. Find the longest repeated substring using the suffix array

Return the longest repeated substring. If there are multiple substrings of the same maximum length, return the lexicographically smallest one.

Note: A repeated substring must appear at least twice in the string.

Input & Output

Example 1 — Classic Case
$ Input: s = "banana"
Output: "ana"
💡 Note: The suffixes are ["banana", "anana", "nana", "ana", "na", "a"]. After sorting: ["a", "ana", "anana", "banana", "na", "nana"]. The longest common prefix between adjacent suffixes "ana" and "anana" is "ana" with length 3.
Example 2 — Multiple Repetitions
$ Input: s = "abcabc"
Output: "abc"
💡 Note: The string contains "abc" repeated twice. The suffix array helps identify this as the longest repeated substring with length 3.
Example 3 — No Repetition
$ Input: s = "abcd"
Output: ""
💡 Note: All characters are unique, so no substring repeats. The longest repeated substring is empty.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters only

Visualization

Tap to expand
INPUTALGORITHMRESULTString: "banana"bananaAll suffixes to process:banana, anana, nanaana, na, aGoal: Find longestrepeated substring1Build Suffix ArraySort all suffixes bylexicographic order2Compute LCP ArrayFind common prefixesbetween adjacent suffixes3Find Maximum LCPLongest common prefix= longest repetitionSorted Suffixes:[a, ana, anana, banana, na, nana]LCP Analysis:a ↔ ana: LCP = "a" (len=1)ana ↔ anana: LCP = "ana" (len=3) ★anana ↔ banana: LCP = "" (len=0)Final Answer:"ana"Key Insight:Sorting all suffixes brings repeated substrings together as adjacent entries in the suffix array.The longest repeated substring corresponds to the maximum LCP value between consecutive suffixes.TutorialsPoint - Suffix Array Builder | Radix Sort Approach
Asked in
Google 45 Microsoft 35 Amazon 30 Meta 25
23.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen