Build an Inverted Document Index
Problem statement
Build an inverted index for an ordered array of documents. Tokenize each document into maximal ASCII letter-or-digit terms, lowercase every term, and associate it with the zero-based IDs of documents containing it.
Return one string per term in ascending term order using term:id1,id2,.... Document IDs are ascending and appear at most once per term.
Function
buildInvertedIndex(documents: String[]) → String[]Examples
Example 1
documents = ["Red blue red","blue green"]return = ["blue:0,1","green:1","red:0"]Each term receives one posting per containing document in document order.
Example 2
documents = ["A a A"]return = ["a:0"]Each term receives one posting per containing document in document order.
Example 3
documents = []return = []Each term receives one posting per containing document in document order.
Constraints
0 <= documents.length <= 10000.- The total input length is at most
10^6. - Documents contain printable ASCII characters.
Source note: Author-written prompt excerpt retained for source fidelity.