Skip to the code
cf.
+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 50,000 characters long, so comparing whole suffixes as strings 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 <= 5 * 10^4
  • text contains lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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