Skip to the code
+25 XP

#631 Sort Every Suffix

medium

A documentation site builds a search index in the browser. Its first step is to sort all suffixes of a page's text, so that any search term can be found by binary search.

Write suffixOrder(text) that returns the start indexes of all suffixes of text, ordered so the suffixes are in increasing alphabetical order (compare character codes; a shorter suffix that is a prefix of a longer one comes first).

Texts can be 100,000 characters long, and in a repetitive text two suffixes can share thousands of letters, so comparing whole suffixes letter by letter may be too slow.

Example 1

Input: text = "banana"
Output: [5,3,1,0,4,2]

a, ana, anana, banana, na, nana.

Example 2

Input: text = "abc"
Output: [0,1,2]

abc, bc, c are already in order.

Constraints

  • 0 <= text.length <= 10^5
  • text contains lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).