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:
- Build a suffix array for the given string in O(n log n) time complexity
- 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code